correct way to inherit method with 1 correction
<p>I experimented a little bit OOP in python, while doing homework on data structures and I have some troubles with understanding how to correct inherit some method with corrections. </p>

<p>So, I have:</p>

<pre><code>class BinarySearchTree(object):
    # snipped
    def _insert(self, key, val):
        prevNode = None
        currentNode = self.root
        while currentNode:
            prevNode = currentNode
            if key &lt; currentNode.key:
                currentNode = currentNode.leftChild
            else:
                currentNode = currentNode.rightChild

        if key &lt; prevNode.key:                      
            prevNode.leftChild = self.Node(key, val, parent=prevNode)
            self.updateNodeProperties(prevNode.leftChild)            
        else:                                       
            prevNode.rightChild = self.Node(key, val, parent=prevNode)
            self.updateNodeProperties(prevNode.rightChild)
    # snipped                
</code></pre>

<p>And: </p>

<pre><code>class RBTree(BinarySearchTree): 
    # snipped
    def _insert(self, key, val):
        prevNode = None
        currentNode = self.root
        while currentNode:
            prevNode = currentNode  
            prevNode.size += 1 # The only difference is in this line
            if key &lt; currentNode.key:
                currentNode = currentNode.leftChild
            else:
                currentNode = currentNode.rightChild

        if key &lt; prevNode.key:                      
            prevNode.leftChild = self.Node(key, val, parent=prevNode)
            self.updateNodeProperties(prevNode.leftChild)            
        else:                                       
            prevNode.rightChild = self.Node(key, val, parent=prevNode)
            self.updateNodeProperties(prevNode.rightChild)
    # snipped
</code></pre>

<p>And there is my question: is there smart way to inherit this method and realise this 1 difference(<code>prevNode.size += 1</code>) without copying whole inherited function code?</p>

<p><em>P.S.: sorry for my bad english.</em></p>

<p>UPD: Now I cannot choose between Scott's and CAB's solutions...</p>

<p>Here's a suggestion using the Visitor design pattern.</p>

<p>In this pattern, you're writing a visitor method than knows specifically what needs to be done with each node visited.</p>

<p>We'll change the base class to add a visitor capability;</p>

<pre><code>class BinarySearchTree(object):
# snipped
def _insert(self, key, val, visitor=None):   # Change here
    prevNode = None
    currentNode = self.root
    while currentNode:
        prevNode = currentNode
        if visitor: visitor(prevNode)       # Change here
        if key &lt; currentNode.key:
            currentNode = currentNode.leftChild
        else:
            currentNode = currentNode.rightChild

    if key &lt; prevNode.key:                      
        prevNode.leftChild = self.Node(key, val, parent=prevNode)
        self.updateNodeProperties(prevNode.leftChild)            
    else:                                       
        prevNode.rightChild = self.Node(key, val, parent=prevNode)
        self.updateNodeProperties(prevNode.rightChild)
# snipped   
</code></pre>

<p>Now your second class;</p>

<pre><code>class RBTree(BinarySearchTree): 
# snipped
def _insert(self, key, val):
    visitor = lambda node: node.size += 1
    super(RBTree, self)._insert(self, key, val, visitor)
# snipped 
</code></pre>

python python-2.7 inheritance