I have the following python code which sets the root of an AVL tree to a specified value. But it seems that setting the root variable of the class has no effect when done by passing it through a function in the same class.
class AVLTree:
class AVLNode:
def __init__(self, value) -> None:
self.value = value
def __init__(self) -> None:
self._root = None
def insert(self, value: int) -> None:
return self._insert(value, self._root)
def _insert(self, value, node):
if node is None:
node = value
return
avl = AVLTree()
avl.insert(5)
print(avl._root)
Prints None
It seems that passing the class variable self._root
as a parameter to a member method does not change it's value.
I read that python passes all class members by reference and only immutable types (int, etc.) as values.
Any idea why i am not able to modify the value of self._root
in _insert
function and how can i do that? Thank you