Extending base class methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • henrikpierrou@hotmail.com

    #1

    Extending base class methods

    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_Collectio n instance as first
    argument (got MSD instance instead)"):


    #============== =============== =============== =============== =============== ===========
    class Field_Collectio n:
    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_Colle ction):
    standard = ""
    decField = ""

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

    #============== =============== =============== =============== =============== ===========

    w2k, python 2.3

  • Steve Juranich

    #2
    Re: Extending base class methods

    On 19 Apr 2005 07:01:10 -0700, henrikpierrou@h otmail.com
    <henrikpierrou@ hotmail.com> wrote:[color=blue]
    > Any ideas why this does not work? I get the error "TypeError: unbound
    > method printer() must be called with Field_Collectio n instance as first
    > argument (got MSD instance instead)"):[/color]

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

    class Field_Collectio n(object):
    # Some stuff.

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


    HTH

    --
    Steve Juranich
    Tucson, AZ
    USA

    Comment

    • henrikpierrou@hotmail.com

      #3
      Re: Extending base class methods

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

      /H

      Comment

      • Steve Juranich

        #4
        Re: Extending base class methods

        On 19 Apr 2005 08:27:28 -0700, henrikpierrou@h otmail.com
        <henrikpierrou@ hotmail.com> wrote:[color=blue]
        > Ok, i'll try that. But what about the recommendation in the tutorial,
        > is that not possible?[/color]

        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:
        [color=blue][color=green][color=darkred]
        >>> from Foo import *
        >>> f = Foo()
        >>> f.foo()[/color][/color][/color]
        foo[color=blue][color=green][color=darkred]
        >>> b = Bar()
        >>> b.foo()[/color][/color][/color]
        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

        Comment

        • Sizer

          #5
          Re: Extending base class methods

          henrikpierrou@h otmail.com wrote in
          news:1113919270 .802463.54310@l 41g2000cwc.goog legroups.com:[color=blue]
          > Any ideas why this does not work? I get the error "TypeError: unbound
          > method printer() must be called with Field_Collectio n instance as
          > first argument (got MSD instance instead)"):
          >
          >
          > #============== =============== =============== =============== ===========
          > =============== class Field_Collectio n:
          > 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_Colle ction):
          > standard = ""
          > decField = ""
          >
          > def printer(self):
          > print "Standard: " + self.standard
          > print "decField: " + self.decField
          > Field_Collectio n.printer(self)
          >
          > #============== =============== =============== =============== ===========
          > ===============
          >
          > w2k, python 2.3
          >[/color]


          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()

          Comment

          Working...