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>It's not exactly elegant, but you could do something like this:</p>

<pre><code>class BinarySearchTree(object):
    def _insert(self, key, val, update=False):
       prevNode = None
       currentNode = self.root
        while currentNode:
            prevNode = currentNode
            if update:
                prevNode.size += 1
            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  

class RBTree(BinarySearchTree): 
    # snipped

    def _insert(self, key, val):
        super(RBTree, self)._insert(self, key, val, update=True)
</code></pre>

<p>I don't really like that, because it has code in your <code>BinarySearchTree</code> class to update an instance variable that doesn't exist except in the derived <code>RBTree</code> class, but since that part of the code shouldn't ever get executed for a <code>BinarySearchTree</code> instance, you shouldn't encounter a <code>NameError</code>...</p>

python python-2.7 inheritance