By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Inheritance problem

P: n/a
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...

May 9 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2007-05-09, am************@gmail.com <am************@gmail.comwrote:
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
May 9 '07 #2

P: n/a
am************@gmail.com wrote:
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

--
BOFH excuse #108:

The air conditioning water supply pipe ruptured over the machine
room

May 9 '07 #3

P: n/a
On May 9, 11:33 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
amidzic.bra...@gmail.com wrote:
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__()
# [...]

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.

--
Regards,
Steven
May 9 '07 #4

P: n/a
On May 9, 12:09 pm, amidzic.bra...@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

May 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.