{Q} OOP Animal class.

Rainer Deyke root at rainerdeyke.com
Mon Jun 19 19:36:47 EDT 2000


<johnvert at my-deja.com> wrote in message news:8iluf0$le$1 at nnrp1.deja.com...
> 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

You missed a couple of steps.  Here's my interpretation of what happened.
Note that this is fairly specific to Python.  Most OO languages work
differently.

1. You try to access data.reply.
2. data.reply does not exist as an instance variable, so look for
Hacker.reply.
3. Hacker.reply does not exist, so look for Primate.reply.
4. Primate.reply does not exist, so look for Mammal.reply.
5. Mammal.reply does not exist, so look for Animal.reply.
6. Animal.reply exists, and is a function, so a bound method is created.
7. You call the bound method with no argument.
8. The call to to the bound method is translated into a call to Animal.reply
with the argument data.
9. Within Animal.reply, you try to access self.speak.
10. self.speak does not exist as an instance variable, so look for
Hacker.speak.
11. Hacker.speak exists, and is a function, so a bound method is created.
12. You call the bound method with no argument.
13. The call to the boundmethod is translated into a call to Hacker.speak
with argument self.
14. Within Hacker.speak, 'Hello World!' is printed.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware action/role-playing games      -      http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list