Python: how to inherite and override

Posted by Guy on Stack Overflow See other posts from Stack Overflow or by Guy
Published on 2010-05-16T09:17:02Z Indexed on 2010/05/16 9:20 UTC
Read the original article Hit count: 270

Filed under:
|
|

Consider this situation:

I get an object of type A which has the function f. I.e:

class A:
   def f():
      print 'in f'
   def h():
      print 'in h'

and I get an instance of this class but I want to override the f function but save the rest of the functionality of A. So what I was thinking was something of the sort:

class B(A):

     ....

     def f():
         print 'in B->f'

and the usage would be:

def main(a):
   b = B(a)
   b.f()   #prints "in B->f"
   b.h()   #print "in h"

How do you do such a thing?

© Stack Overflow or respective owner

Related posts about python

Related posts about inheritance