0

Sorry if this has been asked before, but I'm normally a java programmer and I'm trying to wrap my head around this whole self business in python. I understand that self is needed to differentiate class variables from instance variables, but what about functions? For example, I'm implementing a binaryTree class. Of course, there are simpler ways, but I'm trying to teach a student about classes, so that's why I went this route.

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

class Tree:
    def __init__(self):
        self.root = None

    def _insert(self, root, value):
        if value < root.value:
            if root.left == None:
                root.left = Node(value)
            else:
                self._insert(root.left, value)

        if value > root.value:
            if root.right == None:
                root.right = Node(value)
            else:
                _insert(root.right, value)
        return


    def insert(self, value):
        if self.root == None:
            self.root = Node(value)
        else:
            _insert(self.root, value)

As you can see, the binaryTree class has an insert() function to add a new node, it also has _insert() which basically acts as a private function. What I'm curious about is that the very last line of this snippet will give me an error: "NameError: name '_insert' is not defined", unless I call it as self._insert(self.root, value). Shouldn't the _insert() function work even if it's not called as part of the instance (i.e. using 'self')? I thought it would behave like a static method, so why is self needed for that function call? Why is the function required to be part of the instance?

thanks!

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    Does this answer your question? [What is the purpose of the word 'self'?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self) – Vinni Marcon Sep 16 '20 at 07:52
  • "I thought it would behave like a static method, so why is self needed for that function call?" - first, why would you expect it to behave like a static method? It isn't one. Second, even if it was a static method, you would have had to do `Tree._insert(root.right, value)`, not `_insert(root.right, value)`. – user2357112 Sep 16 '20 at 07:54
  • No, this simply does not work like Java, where you can refer to any member with or without using `this`. You must always use an object (e.g. `self`) to refer to its members, even if it's the object you're "in". Anything else is interpreted as a global name (or, well, some name in scope…). – deceze Sep 16 '20 at 07:54
  • You could in fact do `Tree._insert(self, root.right, value)` although that would not be pythonic. – alani Sep 16 '20 at 07:56
  • You are still able to use general functions defined outside of the class inside it. So if you don't use `self.` Python will look for a function named `_insert` which there is none. Because the "function" `_insert` you defined is actually an unbound instance method. This is why you need to call it with `self.` to be able to call the specific method bound to that instance. You can also do `Tree._insert(self, ...)` but there is really no reason to do that – Tomerikoo Sep 16 '20 at 07:56
  • Another related question https://stackoverflow.com/questions/5615648/python-call-function-within-class – Tomerikoo Sep 16 '20 at 07:59
  • Ah, I think I get it now. So class methods aren’t declared like class variables by just positioning them in the right spot. Looks like class and static methods require an @ declaration. Python definitely takes some getting used to! Thanks for the feed back guys. – William Tuttle Sep 16 '20 at 10:21

0 Answers0