Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Final Exam Review Spring 2003: Defining a Class

Questions, answers, comments?

(Back to Sp2003 Final Exam Review)



How about this?

class address:
def __init__(self, namein, numberin, addressin):
self.name=namein
self.number=numberin
self.address=addressin

def printname(self):
print(self.name)

def printnumber(self):
print(self.number)

def printaddress(self):
print(self.address)

Chris Sung

Very nice, but not exactly the spec. Check again what the names of the methods are. Mark Guzdial


class address:
  def __init__(self, name, number, address):
    self.name=name
    self.number=number
    self.address=address
  def name(self):
    print(self.name)
  def number(self):
    print(self.number)
  def address(self):
    print(self.address)

This is his code slightly modified like I think is necessary... just changed the printnumber to number and so on. Katie Graybeal

Right, Katie. It's still not return-ing, though. Also, note that I changed the spec a little. I'm not sure that you can have an instance variable and a method with the same name in Python. Try "getName" for the method. (We did this at review yesterday, that's why the issue came up.) Mark Guzdial

Can anyone who was at the review yesterday please post the code you got for this question?
Nah, don't do that – generate it! The above is VERY close! Mark Guzdial


NO idea if this is even close, but I am trying. Any suggestions?
Kelly Farrell


class address:
  def __init__(self, name, number, address):
    self.(getName)=name
    self.(getNumber)=number
    self.(getAddress)=address
  def name(self):
    print(self.name)
    return name
  def number(self):
    print(self.number)
    return number
  def address(self):
    print(self.address)
    return address


Not really. You always have to have self.instance_variable if you want to reference inside an object. You don't need the prints, and it's okay to use the variables name, phone, and address. Try this: Mark Guzdial

class address:
  def __init__(self, aname, aphone, anaddress):
    self.name=aname
    self.phone = aphone
    self.address=anaddress
  def getName(self):
     return self.name
  def getPhone(self):
     return self.phone
  def getAddress(self):
    return self.address

Does that make sense?


yes, i get it! you have to use return and print isn't necessary. does it matter if you say "aname" or just "name"? Kelly Farrell

aname is just the input to __init__. name is the instance variable. Mark Guzdial


When I try to do the slides on the classes, I get this error message after creating the class slide and making the playslideshow()

TypeError: this constructor takes no arguments


Did you make sure that you entered __init__ as underscore-underscore-i-n-i-t-underscore-underscore? If it's not exactly that, it won't work. Mark Guzdial


Okay, they have two underscores on either side and I'm still getting errors. The new one says

An error occurred attempting to pass an argument to a function.

....in function __init__
in file /Users/csl/__compile__/media.py, on line 0, in function makeSound
ValueError:
and it gives me the path to the file. My code says:
class slide:
  def __init__(self, pictureFile,soundFile):
    self.picture=makePicture(pictureFile)
    self.sound=makeSound(soundFile)

  def show(self):
    show(self.picture)
    blockingPlay(self.sound)

def playslideshow():
  slide1=slide(getMediaPath("barbara.jpg",),getMediaPath("bassoon-c4.wav"))
  slide2=slide(getMediaPath("santa.jpg",),getMediaPath("bassoon-e4.wav"))
  slide1.show()
  slide2.show()


I've been trying to execute the playslideshow portion.

Nevermind.... I'm such a ditz... I don't have to have them in the same file and I assumed that since it was in the slides, it was in the mediasources. I was wrong. I had to choose different sounds. It worked now.... =)

How do we assume that for the test you understand that we mean TWO underscores?????

Make it really clear? Write a note? We're really not going to be that picky. Mark Guzdial


class address:

def __init__(self, name, phone, address):
self.name=name
self.phone=phone
self.address=address

def getName(self):
return self.name
def getPhone(self):
return self.phone
def getAddress(self):
return self.address

I'm pretty sure this is what was noted as the correct answer in the review session last night.


As Prof. Guzdial says, "I'm not sure that you can have an instance variable and a method with the same name in Python." I think that he changed the instance variables to include "a" to make sure that method names and instance variables don't get confused. That's why the above isn't right. Think about it, it could be confusing to us, and even more confusing to a computer.



Link to this Page