1

I'm trying to teach myself Python, coming from C++, so I decided to try and build a simple BST. I managed to get my insert method working correctly, but I do not know why my printTree method fails. The interpreter gives me the error:

Traceback (most recent call last): File "test.py", line 40, in myTree.printTree() File "test.py", line 23, in printTree printTree(self.left) NameError: global name 'printTree' is not defined

code:

class Node(object):                                                                                                                                                                  

    def __init__(self, value):                                                                                                                                                       
        self.value = value                                                                                                                                                           
        self.left = self.right = None                                                                                                                                                                                                                                                                                                                 

    def insert(self, node):                                                                                                                                                          
        if self is None:                                                                                                                                                             
            self = node                                                                                                                                                              
        else:                                                                                                                                                                        
            if node.value <= self.value:                                                                                                                                             
                if self.left: insert(self.left, node)                                                                                                                                
                else: self.left = node                                                                                                                                               
            else:                                                                                                                                                                    
                if self.right: insert(self.right, node)                                                                                                                              
                else: self.right = node                                                                                                                                              

    def printTree(self):                                                                                                                                                             
        if not self:                                                                                                                                                                 
            return                                                                                                                                                                   
        else:                                                                                                                                                                        
            printTree(self.left)                                                                                                                                                     
            print(self.value)                                                                                                                                                        
            printTree(self.right)

if __name__ == "__main__":
    myTree = Node(3)
    myTree.insert(Node(2))
    myTree.insert(Node(4))

    myTree.printTree()

Can I not pass the current instance this way?

2
  • Btw you also have the same problem with insert and your reference to root in printTree. Commented Feb 20, 2015 at 7:54
  • @lightalchemist root was a typo (fixed). My insert function seems to work when I write the printTree function explicitlyprintTree(root) and then printTree(myTree), which is why I'm confused. Commented Feb 20, 2015 at 8:08

1 Answer 1

2

Your recursive calls to printTree in the method should be self.printTree(). Default scoping is different in Python to C++ !

Whereas in C++ the default scope is the current object (*this) that is not the case in Python. The default (unqualified) scope is the same whether we are dealing with a global function or a method.

Sign up to request clarification or add additional context in comments.

5 Comments

Yep, no implicit 'this' on functions called within a class. Hence the inclusion of a 'self' parameter on all non-static class definitions. To quote the 'Zen of Python' - "Explicit is better than implicit".
Okay, but didn't I pass the object implicitly on my insert function? Sorry, having trouble wrapping my head around it.
@Confused: I see what you mean, but are you sure that is being executed? One of the differences to get your head around is that function names are just identifiers - variables if you will. Therefore they cannot be resolved by Python until runtime. So errors like this cannot be detected at compilation time. Therefore, compilation does not mean that your code is correct! As lightalchemist notes above, you have several occurrences of the error.
@cdarke Awesome, figured it out. Something funky was going on that made it seem like insert was working. Thanks for the help!
@Confused: no probs - I have trod the path from C++ to Python myself and understand your confusion. Going from C++ to Python is like being released from a straightjacket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.