title: Python: how to inherit and override
body: <p>Consider this situation:</p>
  
  <p>I get an object of type <code>A</code> which has the function <code>f</code>:</p>
  
  <pre><code>class A:
     def f(self):
        print 'in f'
     def h(self):
        print 'in h'
  </code></pre>
  
  <p>and I get an instance of this class, but I want to override the <code>f</code> function, yet save the rest of the functionality of <code>A</code>. So what I was thinking was something of the sort:</p>
  
  <pre><code>class B(A):
       def __init__(self, a):
          #something here
       ....
  
       def f(self):
           print 'in B-&gt;f'
  </code></pre>
  
  <p>and the usage would be:</p>
  
  <pre><code>def main(a):
     b = B(a)
     b.f()   #prints "in B-&gt;f"
     b.h()   #print "in h"
  </code></pre>
  
  <p>What I want is a sort of copy constructor that gets a parent of the current class (<code>A</code>), and returns an instance of this class (<code>B</code>).</p>
  
  <p>How do you do such a thing? How would the <code>__init__</code> method look?</p>
  
  <p><strong>Note</strong>: this post has been edited by the original poster to incorporate changes suggested in the comments, which is why some of the suggestions look redundant or incorrect.</p>