Inheritance problem

Neil Cerutti horpner at yahoo.com
Wed May 9 14:17:39 EDT 2007


On 2007-05-09, amidzic.branko at gmail.com <amidzic.branko at gmail.com> wrote:
> I'm trying to solve a problem using inheritance and
> polymorphism in python 2.4.2

It's not an inheritance problem, it's a notation problem. Python
uses explicit 'self', saving you the trouble of devising a naming
convention for data members.

> 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

You need to use

 self.a = [1, 2, 3]
 print self.a

The notation you used creates a local variable, but you need a
data member.

> class longList(shortList):
>     def __init__(self):
>         shortList.setList()
>         self.setList()
>
>     def setList(self):
>         a.extend([4,5,6])
>         print a

Similarly:

 self.a.extend([4, 5, 6])
 print self.a

Does that give you better results?

-- 
Neil Cerutti
If we stay free of injuries, we'll be in contention to be a healthy team.
--Chris Morris



More information about the Python-list mailing list