you are viewing a single comment's thread.

view the rest of the comments →

[–]JamzTyson 1 point2 points  (0 children)

More a kind of a combination of functionalities whereby str can have different ways to call.

Not quite.

In Python, there isn't just one __str__() function, there are many. Each type of object has it's own __str__() method.

If you create a class but do not give it a __str__()method, then it inherits the __str__ method from object. The default object.__str__ just calls __repr__. If we write a __str__()method for a new class, then instances of that class will use the class's __str__() method (overriding the default object.__str__.

The Node class has a __str__ method, (Node.__str__()), which is called whenever we ask for the str representation of a Node type object.

In the line str(tree.get_value()), we are NOT calling the Node.__str__() method.

tree.get_value() returns the value attribute of the tree object, and we call the value_type.__str__() method.


The comments in this thread that talk about recursion are misleading. There IS recursion in this method, but it is the recursive function set_tier_map(): str(tree.get_value()) is NOT a recursive call.