Source: 📖 Python Cookbook ch8.1 p244
__str__
methodThe __str__
method indicates what should be printed to the console when print()
is called on an object.
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f'x is {self.x}, y is {self.y}'
p = Point(2, 3)
print(p) # x is 2, y is 3