If I have an instance of the class below, since its constructor has a value and a list, if I print it out, what does it print? a value or a list?
URL is below
# Define your "TreeNode" Python class below
class TreeNode:
def __init__(self, value):
self.value = value
self.children = []
You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility
When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!
If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer!
Without adding your own __str__ or __repr__ it’ll probably default to the following (quote from the link):
“otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object”.
By adding a __str__ method you could get a nice pretty print version which returns what you want but you’d need to add that code yourself. __repr__ is another option but it is used more for debugging with ithe ideal output being something you could use to recreate that object by calling eval() on the output string. Details- https://docs.python.org/3.8/reference/datamodel.html?highlight=__repr__#object.__repr__
It’s just the address of that instance that is printed out.
Like this: <main.TreeNode object at 0x7f11e2290ee0>
If you want to print its value or its list, you have to define repr