Back to questions
JuniorTheoryCommon
What does the program print?
class Point:
def __init__(self, x):
self.x = x
def __repr__(self):
return f"Point({self.x})"
def __str__(self):
return f"point {self.x}"
print(Point(1), [Point(2)])point 1 [Point(2)]
- Expecting
[point 2], as ifprintused__str__for everything: a list shows its items with__repr__, so thePointinside it prints asPoint(2). - Expecting
Point(1)because__repr__comes first in the class: the order of methods does not matter,printcalls__str__on an object it gets directly.
- What would
print(Point(1))show if the class had only__repr__? - What does
print(f"{Point(3)!r}")print, and why?