{Q} OOP Animal class.

johnvert at my-deja.com johnvert at my-deja.com
Mon Jun 19 16:06:01 EDT 2000


Greetings,

I just the example and explanation of the classic Animal class in the
tutorial in the end of Programming Python by Orielly, and I have a few
questions.

This is the class (irrelevant parts snipped):

class Animal:
  def reply(self):  self.speak()
  def speak(self):  print 'spam'

class Mammal(Animal):
  def speak(self):  print 'huh?'

class Primate(Mammal):
  def speak(self):  print 'Hello world!'

class Hacker(Primate):
  pass

>>> data = Hacker()
>>> data.reply()
Hello world!

Programming Python says: ``Hacker has no methods of its own, but
inherits from Primate, which inherits from Mammal, which inherits from
Animal. When we call the reply method of the Hacker instance, it runs
Animal's reply, which in turn calls the Primate's speak method
(self.speak finds the first speak by looking at Hacker and above).
Finally, our message gets printed.'', p. 846

I want to make sure I understand the sequence of events right:

1. I call the method reply()
2. The interperter sees reply() does not exist in class Hacker
3. It goes one class up the hierarchy to class Primate
4. reply() doesn't exist in class Primate
5. It goes one class up the hierarchy to class Mammal
6. reply() doesn't exist in class Mammal either
7. It goes one class up the hierarchy to class Animal
8. reply() exists in class Animal, it calls it, and self.reply() gets
translated to `Primate.reply()' since Hacker is of type Primate
9. Primate.reply() is called
10. `Hello world!' is printed

Is that about right? The thing that I'm most unsure of is step #8..
hoping you could clarify.  I really want to have a solid understanding
of this because it's important to understand OOP.

Thanks,
--
John




Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list