Extending base class methods

2005-04-19 Thread henrikpierrou
Hi,

I am trying to extend an overridden base class method (printer) by
printing some extra fields and then calling the base class method.
Extract from the python tutorial:

'An overriding method in a derived class may in fact want to extend
rather than simply replace the base class method of the same name.
There is a simple way to call the base class method directly: just call
"BaseClassName.methodname(self, arguments)". '


Any ideas why this does not work? I get the error "TypeError: unbound
method printer() must be called with Field_Collection instance as first
argument (got MSD instance instead)"):


#=
class Field_Collection:
fieldList = []

def add(self, name, size, compression, responseValue, value,
description):
self.fieldList.append( Field(name, size, compression,
responseValue, value, description) )

def update(self):
print "updating field"

def get(self):
print "getting field"

def printer(self):
for x in self.fieldList:
x.printer()


#=


class MSD(Field_Collection):
standard = ""
decField = ""

def printer(self):
print "Standard: " + self.standard
print "decField: " + self.decField
Field_Collection.printer(self)

#=

w2k, python 2.3

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extending base class methods

2005-04-19 Thread henrikpierrou
Ok, i'll try that. But what about the recommendation in the tutorial,
is that not possible?

/H

-- 
http://mail.python.org/mailman/listinfo/python-list


Python instances

2005-04-20 Thread henrikpierrou
Hi,

How do python instances work?
Why does the code at the end of my posting produce this output:

list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

instead of

list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list in b:
[]



class MyClass:
list = []

def add(self, x):
self.list.append(x)

def printer(self):
print self.list

a = MyClass()
b = MyClass()

for n in range(10):
a.add(n)

print "list in a:"
a.printer()
print "list in b:"
b.printer()

/H

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python instances

2005-04-20 Thread henrikpierrou
Guess i shouldn't think of the __init__(self) function as a constructor
then.
Thanks.

/H

-- 
http://mail.python.org/mailman/listinfo/python-list