Possibly a stupid question regarding Python Classes

John Hunter jdhunter at ace.bsd.uchicago.edu
Sun Nov 17 11:28:26 EST 2002


>>>>> "Adonis" == Adonis  <deltapigz at telocity.com> writes:


    Adonis> So in essence I want to override the Bar function, but as
    Adonis> well do what it would if it were instantiated. I know I
    Adonis> could instantiate Foo prior to printing Bar, but was
    Adonis> wondering if there was a way to keep it within the same
    Adonis> process.


If I undersand your question correctly, you can do this by calling
Foo.Bar with the self argument passed as the first parameter

class Foo:
    def Bar(self):
        print 'Foo'

class Fib(Foo):
    def Bar(self):
        Foo.Bar(self)
        print 'Bar'

f = Fib()
f.Bar()


This comes up a lot in calling a parent classes initialization function

class Bar:
   def __init__(self,x):
      self.x = x

class Foo(Bar):
   def __init__(self, x, y):
      Bar.__init__(self, x)
      self.y = y

f = Foo(1,2)
print f.x, f.y


John Hunter




More information about the Python-list mailing list