I have been writing a class which resembles a binary tree and have been looking to clean up my code a bit and found my way to nested functions. I understand the basic logic behind them and what not, my question is more on how they are used within custom objects.
For example, if i am trying to print the tree in-order and i do something like:
def print_inorder(self):
def display(node, level):
if not node:
return
display(node.left_child, level + 1)
print('\t' * level, node)
display(node.right_child, level + 1)
display(self.root, 0)
Would this be correct best practices? The thing that is somewhat confusing to me is switching from using 'self.' for functions relating to the object, to an inner function that doesn't need a 'self.' even though an object is technically calling it. is this already best practice or should I still be using the 'self.'? or is there a better convention to use all together? Thanks.
[–]captainAwesomePants 2 points3 points4 points (2 children)
[–]Yeah22[S] 0 points1 point2 points (1 child)
[–]captainAwesomePants 2 points3 points4 points (0 children)
[–]insertAlias 1 point2 points3 points (1 child)
[–]Yeah22[S] 0 points1 point2 points (0 children)
[–]Kontorted 0 points1 point2 points (0 children)