When should you implement __repr__ versus __str__?
PICTURE THIS: RAG CHATBOT
Simple meaning
__repr__ should be unambiguous and, if practical, look like a constructor call for debugging.
WHY — Dunder Methods instead of guessing?
Why interviewers care about Dunder Methods:
contrast on Dunder Methods, not two memorised paragraphs.
the developer, then one case where picking wrong hurts.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1__repr__ should be unambiguous
and, if practical, look like a constructor call for debugging.
- 2__str__ is the pretty,
user-facing form.
- 3If only one is
defined, str() may fall back to __repr__, so at least implement __repr__ on model-like objects.
- 4Give an example
One tiny concrete case you can say aloud.
- 5Common mistake
What juniors usually get wrong.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
__repr__ should be unambiguous and, if practical, look like a constructor call for debugging. __str__ is the pretty, user-facing form.