Inheritance problem

A

amidzic.branko

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...
 
N

Neil Cerutti

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?
 
B

Bjoern Schliessmann

class longList(shortList):

def __init__(self):

shortList.setList()

self.setList()

Addition: Always call the base class __init__ in your constructor if
there exists one, i. e.

class longList(shortList)
def __init__(self):
shortlist.__init__()
# [...]

Regards,


Björn
 
A

attn.steven.kuo

class longList(shortList):
def __init__(self):

self.setList()

Addition: Always call the base class __init__ in your constructor if
there exists one, i. e.

class longList(shortList)
def __init__(self):
shortlist.__init__()
# [...]


Delegating to an ancestor class by
calling an unbound method is fine as
long as one remembers to pass an instance
as the first argument. So, this means:

shortList.setList(self)

and

shortList.__init__(self)

for the examples above.
 
J

Jason

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:
.... def __init__(self, name):
.... self.name = name # Save the name for later
.... def Talk(self):
.... print "Hi,", self.name
....
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.
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:
.... def __init__(self, name):
.... SayHi.__init__(self, name) # Call the superclass's init
.... def Talk(self):
.... print "Hello,", self.name
....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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top