Is there a way to define how to print a class object like in C++ ?
So if I have a code like this
it would print the 'value'Code:class node: def __init__(self,value=0,left=None,right=None): self.value = value self.left = left self.right = right n1 = node(4) print(n1)
overload the str method. It's called whenever a string representation of the object is needed, like in a print statement.
Thanks, that did the trick.
Here's an example in case someone else is pondering the problem:
Code:class oldPerson: def __init__(self,age): self.age = age def __str__(self): return str(self.age) paps = oldPerson(102) print(paps)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks