For types that have a literal notation (str
, int
, float
, list
, etc.) the repr()
returns a string that can be used to create the type again.
That is nice, but not a requirement.
The main purpose for __repr__
is to provide the developer with unambiguous information as to what object they have here, for debugging purposes.
Quoting from the __repr__ documentation:
Called by the repr()
built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...>
should be returned.
but most of all:
This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
Emphasis mine.
Returning a constant string from your custom type would not break anything technically, but would make your life as a developer harder.