Consider:
class Base:
def __init__(self):
self.__repr__ = lambda: "String!"
class Child(Base):
pass
c = Child()
print(f"{c}")
print(f"{c.__repr__()}")
This results in:
<__main__.Child object at 0x7f07cd88f850>
String!
I get the same output when changing the __str__
and __format__
methods. I would like for Child
's representation in an f-string to be just "String!"
, but changing __str__
, __repr__
, and __format__
doesn't achieve this.
What does Python use to determine what's displayed in an f-string if not the instance's __repr__
or either of the other two methods?