Hi
Level: Beginner
I'm doing my first steps in Object Oriented programming. The code is aimed at showing how methods are passed up the chain. So when i call UG.say(person, 'but i like')
the method say
is instructed to call class MITPerson
. Given that MITPerson
does not contain a say
method it will pass it up to class Person
. I think there is nothing wrong with the code as it is part of a lecture (see source below). I think it is me who is omitting to define something when i run the code. Not sure what though. I think that the UG instance
the error message is looking for as first argument is refering to self
but that, in principle, doesn't need to be provided, correct? Any hints?
class Person(object):
def __init__(self, family_name, first_name):
self.family_name = family_name
self.first_name = first_name
def familyName(self):
return self.family_name
def firstName(self):
return self.first_name
def say(self,toWhom,something):
return self.first_name + ' ' + self.family_name + ' says to ' + toWhom.firstName() + ' ' + toWhom.familyName() + ': ' + something
class MITPerson(Person):
def __init__(self, familyName, firstName):
Person.__init__(self, familyName, firstName)
class UG(MITPerson):
def __init__(self, familyName, firstName):
MITPerson.__init__(self, familyName, firstName)
self.year = None
def say(self,toWhom,something):
return MITPerson.say(self,toWhom,'Excuse me, but ' + something)
>>> person = Person('Jon', 'Doe')
>>> person_mit = MITPerson('Quin', 'Eil')
>>> ug = UG('Dylan', 'Bob')
>>> UG.say(person, 'but i like')
UG.say(person, 'bla')
**EDIT (for completeness)**: it should say UG.say(person, 'but i like') #the 'bla' creeped in from a previous test
TypeError: unbound method say() must be called with UG instance as first argument (got Person instance instead)
source: MIT OpenCourseWare http://ocw.mit.edu Introduction to Computer Science and Programming Fall 2008