refering to base classes
Chaz Ginger
cginboston at hotmail.com
Tue Aug 29 12:45:51 EDT 2006
Jason wrote:
> Chaz Ginger wrote:
>> Chaz Ginger 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:"
>> I did forget to mention that in 'dog"s' __init__ you had better call
>> creature's __init__. You might make it look like this:
>>
>> def __init__(self):
>> self.noise = 'bark'
>> creature.__init__(self)
>>
>
> There's a problem with Chaz's __init__() method. Notice that the
> creature class's __init__ sets self.noise to the empty string. In this
> case, the superclass's __init__() method should be called first:
>
> class dog(creature):
> def __init__(self):
> creature.__init__(self)
> self.noise = "bark"
> def voice(self):
> print "brace your self:"
> creature.voice(self)
>
> --Jason
>
Very true....I was showing him in "spirit only"...lol.
Chaz.
More information about the Python-list
mailing list