refering to base classes

glenn glenn at tangelosoftware.net
Tue Aug 29 23:48:57 EDT 2006


Hi Roberto
> If you want dog.voice() to just print "voice: bark", you just have to omit
> the voice method for the dog class: it will be inherited from creature.
>
I would have thought this would be correct, but in this case, plus in
others im playin with, I get this issue:
-----------------------
given animal.py is:
class creature:
    def __init__(self):
        self.noise=""
    def voice(self):
        return "voice:" + self.noise

class dog(creature):
    def __init__(self):
        self.noise="bark"

then I get this outcome...
>>>import animal
>>> beagle=animal.dog
>>> beagle.voice()
Traceback (most recent call last):
  File "<input>", line 1, in ?
TypeError: unbound method voice() must be called with dog instance as
first argument (got nothing instead)
>>>
------------------------
So I guess it wants something in position of self?

any idea what Im doing wrong? - this would be very handy as its a point
Im stymied on a couple of 'projects'
thanks
Glenn
> If you want dog.voice() to do something else, you can call superclass'
> method like this:
>
>     def voice(self):
>         creature.voice(self)
>         print "brace your self"
>         any_other_magic()
> 

> HTH
> -- 
> Roberto Bonvallet




More information about the Python-list mailing list