Python·100 questions

How to correctly implement __repr__?

Answer
Make an unambiguous representation.
Ideally: so that the object can be reconstructed (if really possible).
Use f'ClassName(x={self.x!r})'.
Do not perform heavy computations.
For dataclass, __repr__ is generated automatically.
Was this answer helpful?

More questions in this topic

Related questions from other topics