Inheritance problem

Jason tenax.raccoon at gmail.com
Wed May 9 15:09:52 EDT 2007


On May 9, 12:09 pm, amidzic.bra... at gmail.com wrote:
> I'm trying to solve a problem using inheritance and polymorphism in
> python 2.4.2
>
> I think it's easier to explain the problem using simple example:
>
> class shortList:
>
>     def __init__(self):
>
>         self.setList()
>
>     def setList(self):
>
>         a = [1,2,3]
>
>         print a
>
> class longList(shortList):
>
>     def __init__(self):
>
>         shortList.setList()
>
>         self.setList()
>
>     def setList(self):
>
>         a.extend([4,5,6])
>
>         print a
>
> def main():
>
>     a = raw_input('Do you want short or long list? (s/l)')
>
>     if a.upper() == 'S':
>
>         lst = shortList()
>
>     else:
>
>         lst = longList()
>
>     lst.setList()
>
> if __name__ == '__main__':
>
>     main()
>
> After that I'm getting a message:
>
> TypeError: unbound method setList() must be called with shortList
> instance as first argument (got nothing instead)
>
> Where is the problem?
>
> Thanks in advance...

As Neil indicated, you use the self object to access the current
object's class members.  The first parameter to a Python method is the
object that the method is operating on.  For a silly example:

>>> class SayHi(object):
...     def __init__(self, name):
...         self.name = name  # Save the name for later
...     def Talk(self):
...         print "Hi,", self.name
...
>>> helloWorld = SayHi("World")
>>> helloWorld.Talk()
Hi, World
>>>

As you can see, the __init__ special method saves the name by
"self.name = name".  Similarly, the name is accessed in the Talk
method via "self.name".

Please note that the first parameter is called "self" by convention.
Python doesn't care what you call the first parameter to a method.
However, most of the Python world uses "self", so it is highly
recommended to follow this convention.

Now for the problem specific to the error message that you're getting:

As above, the first parameter to a Python method is the object being
operated on.  Python uses a little bit of "syntactic sugar" to make
calling methods easier.  You can explicitly call a class method
directly and pass the object in as the first parameter yourself.

>>> helloWorld.Talk()   # This is the normal way of calling Talk
Hi, World
>>> SayHi.Talk(helloWorld)  # This works, too!
Hi, World

To easily call a superclass method, you can explicitly use the
baseclass.  You're attempting to do so, but you forgot one important
ingrediant: the object being operated on!  In C++, the object is
implicit.  In Python, you must explicitly send it to the superclass:

>>> class SayHello(SayHi):
...     def __init__(self, name):
...         SayHi.__init__(self, name)  # Call the superclass's init
...     def Talk(self):
...         print "Hello,", self.name
...
>>> moreTalk = SayHello("World")
>>> moreTalk.Talk()
Hello, World

Basically, you need to:
  1.  Assign your lists in setList to "self.a" instead of the local
variable "a".
  2.  Pass the object to the superclass method in setList
("shortList.setList(self)")

There are other things that may be improved with your design.  Keep
plugging at it!

    --Jason




More information about the Python-list mailing list