Base-class method access

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Jul 15 23:05:40 EDT 2002


----- Original Message ----- 
From: "Joseph Barillari" <jbarilla at princeton.edu>


Hi.

> Is it possible for a derived class to access overridden methods of its
> base class? For example:
>
>
> class Foo:
>       def frob():
>           print "Spam"

This is an error; frob() needs self declared.  Same error below.

> 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>
> 
> >> a = Bar()
> >> a.frob()
> Eggs
> Spam
> 
> Is there a means of doing this in Python? 

Coded like this:

    class Foo:
        def frob(self):
            print "Spam"

    class Bar(Foo):
        def frob(self):
            print "Eggs"
            Foo.frob(self)
  
    >> a = Bar()
    >> a.frob()
    Eggs
    Spam

works just fine.

Note that you *must* pass self if you call a class method 
directly.

> Thanks in advance.
> 
> --Joe

Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net






More information about the Python-list mailing list