I’m trying to set a function to do something like this
def __binaryTreeInsert(self, toInsert, currentNode=getRoot(), parentNode=None):
where current node starts as root, and then we change it to a different node in the method and recursivly call it again.
However, i cannot get the ‘currentNode=getRoot()’ to work. If i try calling the funcion getRoot() (as above) it says im not giving it all the required variables, but if i try to call self.getRoot() it complains that self is an undefined variable. Is there a way i can do this without having to specify the root while calling this method?
EDIT: The base case of this method is already
if currentNode == None:
so using that to set the root wouldn’t work
While
arg=Noneis the idiomatic Python sentinel value for an non-supplied argument, it doesn’t have to beNone. In Lua, for instance, the idiomatic non-supplied argument is an empty table. We can actually apply that to this case:Output:
The argument was 123 You didn't supply an argument! The argument was None The argument was {}This works for any case except explicitly passing
Foo.sentinel, becauseFoo.sentinelis guaranteed to have a unique address — meaning,x is Foo.sentinelis only true when x isFoo.sentinel🙂 Thus, due to the closure we’ve created aroundFoo.sentinel, there is only one object that can create an ambiguous situation, and it will never be used by accident.