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

Extending base class methods

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

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 19 Apr 2005 07:01:10 -0700, he***********@hotmail.com
<he***********@hotmail.com> wrote:
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

--
Steve Juranich
Tucson, AZ
USA
Jul 19 '05 #2

P: n/a
Ok, i'll try that. But what about the recommendation in the tutorial,
is that not possible?

/H

Jul 19 '05 #3

P: n/a
On 19 Apr 2005 08:27:28 -0700, he***********@hotmail.com
<he***********@hotmail.com> wrote:
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:
from Foo import *
f = Foo()
f.foo() foo b = Bar()
b.foo()

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
--
Steve Juranich
Tucson, AZ
USA
Jul 19 '05 #4

P: n/a
he***********@hotmail.com wrote in
news:11*********************@l41g2000cwc.googlegro ups.com:
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()

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.