refering to base classes
Bruno Desthuilliers
onurb at xiludom.gro
Tue Aug 29 11:27:58 EDT 2006
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
--
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