Base-class method access

Mark McEahern marklists at mceahern.com
Mon Jul 15 23:00:27 EDT 2002


> Is it possible for a derived class to access overridden methods of its
> base class? For example:
>
>
> class Foo:
>       def frob():
>           print "Spam"
>
> class Bar(Foo):
>       def frob():
>           print "Eggs"
>           # In C#, the syntax to call Foo's frob() would be
>           # base.frob()
>           <Insert Python-appropriate syntax for that call here>

Here's the syntax:

            Foo.frob(self)

However, this only makes sense if you fix your definition of frob().  It's
missing the implicit reference to the instance, normally spelled self:

        def frob(self):

You can also look into super(), but the above should work.

Cheers,

// m

-






More information about the Python-list mailing list