Extending base class methods

H

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
 
S

Steve Juranich

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)"):

My suggestion would be to make Field_Collection inheirit from
"object". Then you can make use of the "super" call inside of MSD.
It would look something like this:

class Field_Collection(object):
# Some stuff.

class MSD(Field_Collection):
# More stuff
def printer(self):
print "Standard: " + self.standard
print "decField: " + self.decField
super(MSD, self).printer()


HTH
 
H

henrikpierrou

Ok, i'll try that. But what about the recommendation in the tutorial,
is that not possible?

/H
 
S

Steve Juranich

Ok, i'll try that. But what about the recommendation in the tutorial,
is that not possible?

In the new (2.4) version of the Tutorial, that statement has been
removed. What you're using has been called "old-style" classes for
quite a while now (since 2.0?). Any new Python code should really be
using the "new-style" classes (inherit from "object" and use things
like "super".

But FWIW, it appears to still work with Python 2.4:

#---------- <snip FILE="Foo.py"> -----------------
class Foo:
def foo(self):
print "foo"

class Bar(Foo):
def foo(self):
print "bar"
Foo.foo(self)
#----------- </snip> ------------


# Now in the Python interpreter:
bar
foo

So nothing jumps out to me that you did obviously wrong, sorry. But
in general my advice would be to switch to using "new-style" classes.

HTH
 
S

Sizer

(e-mail address removed) wrote in
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


I added these lines to your code:

foo = MSD()
foo.printer()

And it worked perfectly (python 2.3.3). You would get that error if you
accidentally did something like:

foo = MSD()
MSD.printer() # oops, should be foo.printer()
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top