refering to base classes
glenn
glenn at tangelosoftware.net
Tue Aug 29 23:36:51 EDT 2006
Bruno Desthuilliers wrote:
> glenn wrote:
> > hi - Im quite new to python, wondering if anyone can help me understand
> > something about inheritance here. In this trivial example, how could I
> > modify the voice method of 'dog' to call the base class 'creatures'
> > voice method from with in it?
> >
> > class creature:
> > def __init__(self):
> > self.noise=""
> > def voice(self):
> > return "voice:" + self.noise
> >
> > class dog(creature):
> > def __init__(self):
> > self.noise="bark"
> >
> > def voice(self):
> > print "brace your self:"
>
>
> <ot>
> It might be better to use newstyle classes if you can. Also, the
> convention is to use CamelCase for classes names (unless you have a
> strong reason to do otherwise).
> </ot>
>
> Here you could use a class attribute to provide a default:
>
> class Creature(object):
> noise = ""
>
> def voice(self):
> return "voice:" + self.noise
>
>
> class Dog(Creature):
> noise="bark"
>
> def voice(self):
> print "brace your self:"
> return Creature.voice(self)
> # can also use this instead, cf the Fine Manual
> return super(Dog, self).voice()
>
> My 2 cents
ohh - interesting. Thanks for the camelCase tip - dont have a good
reason to do otherwise, just bad habits.
so for your $.02 do you see this as being, umm, superior in anyway to
creature.voice()?
glenn
> --
> bruno desthuilliers
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
> p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list