[Edu-sig] Getting ready for class...
Andre Roberge
andre.roberge at gmail.com
Thu Mar 9 18:02:51 CET 2006
On 3/9/06, kirby urner <kirby.urner at gmail.com> wrote:
> Here's what I'm starting with today:
>
> http://www.4dsolutions.net/ocn/python/zoo.py
>
> Note: inheriting from object at the top level, per André's suggestion.
>
> Kirby
I like your examples. I imagine you are presenting them to the same
group of students you mentioned before. One example that I've seen on
comp.lang.python which I really like, especially because it has a
second level of inheritance, is the following (original by John
Gabriele, with comments modified slightly here):
#-----------------------------------------------------------------
class Grand_parent( object ):
def speak( self ):
print 'Grand_parent.speak()'
self.advise()
def advise( self ):
print 'Grand_parent.advise()'
self.critique()
def critique( self ):
print 'Grand_parent.critique()'
#-----------------------------------------------------------------
class Parent( Grand_parent ):
def speak( self ):
print '\tParent.speak()'
self.advise()
def advise( self ):
print '\tParent.advise()'
self.critique()
# The Parent inherits his criticism method from his/her own parent
#-----------------------------------------------------------------
class Child( Parent ):
def speak( self ):
print '\t\tChild.speak()'
self.advise()
# Currently, the Child has no really useful advice to give. The
child will just
# parrot what he/she hears the parent say.
def critique( self ):
print '\t\tChild.critique()'
#-----------------------------------------------------------------
print 'speak() calls advise(), then advise() calls critique().'
print
people = [ Grand_parent(), Parent(), Child() ]
for person in people:
person.speak()
print
====================
The output is:
speak() calls advise(), then advise() calls critique().
Grand_parent.speak()
Grand_parent.advise()
Grand_parent.critique()
Parent.speak()
Parent.advise()
Grand_parent.critique()
Child.speak()
Parent.advise()
Child.critique()
André
More information about the Edu-sig
mailing list