Note that there are some explanatory texts on larger screens.

plurals
  1. POobject oriented programming basics: inheritance & shadowing (Python)
    primarykey
    data
    text
    <p>Level: Beginner</p> <p>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 <code>UG.say(person, 'but i like')</code> the method <code>say</code> is instructed to call class <code>MITPerson</code>. Given that <code>MITPerson</code> does not contain a <code>say</code> method it will pass it up to class <code>Person</code>. 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 <code>UG instance</code> the error message is looking for as first argument is refering to <code>self</code> but that, in principle, doesn't need to be provided, correct? Any hints?</p> <pre><code>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) &gt;&gt;&gt; person = Person('Jon', 'Doe') &gt;&gt;&gt; person_mit = MITPerson('Quin', 'Eil') &gt;&gt;&gt; ug = UG('Dylan', 'Bob') &gt;&gt;&gt; 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) </code></pre> <p>source: MIT OpenCourseWare <a href="http://ocw.mit.edu" rel="nofollow">http://ocw.mit.edu</a> Introduction to Computer Science and Programming Fall 2008</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload