[Edu-sig] multiple inheritance and __mro__
kirby urner
kirby.urner at gmail.com
Thu Sep 24 02:53:06 CEST 2009
Here's a lesson plan working directly in the shell, though it'd be
fine to call this up as pre-saved scaffolding.
Sometimes you just wanna project and type live while talking, students
following along, asking questions, or watching on ShowMeDo or one of
those.
>>> class Reptile:
def __init__(self):
self.blood = "cold"
self.me = 1
self.free = True
>>> class Mammal:
def __init__(self):
self.me = 1
self.blood = "warm"
self.free = True
def nurse(self):
return "milk"
>>> class PSF_snake(Mammal, Reptile):
def say(self, something):
print something
>>> me = PSF_snake()
>>> me.blood
'warm'
>>> me.nurse()
'milk'
>>> class PSF_snake(Reptile, Mammal):
def say(self, something):
print something
>>> me = PSF_snake()
>>> me.blood
'cold'
>>> me.nurse()
'milk'
>>>
Notes to teachers:
Notice that it matters in what order the parent classes get listed
(fed to the child class) in the class definition. In the Mammal,
Reptile case, the blood is warm, whereas if Reptile is listed first,
the blood is cold.
Notice also that you get a nursing method either way, because of how
method resolution looks. If Mammal is listed after the Reptile class
then the interpreter simply has to look a little further into its
heritage, in order to find the right DNA.
Kirby
More information about the Edu-sig
mailing list