mediumConceptCodingMidSenior
If you were writing a small value type in Python, which dunder methods would you implement, what would you let a dataclass generate, and how does super() decide what to call?
4 min readUpdated 2026-08-15
Answer
Short answer
Implement the dunders that opt your type into a protocol, treating repr, eq and hash as one decision, and let a dataclass generate the boilerplate when the fields are the identity.
Keep dunder methods explicit in the answer because that is the concept the interviewer is actually trying to test.
Was this answer helpful?
Likely follow-ups
- Why does a frozen dataclass get a working __hash__ when a mutable one does not?
- What does passing slots=True to dataclass change, and what does it break?
- How would you validate field values on construction without writing __init__ by hand?
- When is __init_subclass__ a better tool for enforcing a rule than a metaclass?
Related questions
- Why does a mutable default argument keep the values from previous calls?mediumAlso on dataclasses3 min
- When would you reach for an abstract base class rather than a Protocol, and what does isinstance check in each case?hardAlso on protocols5 min
- When would you choose UDP over TCP, given that TCP is reliable and UDP is not?mediumAlso on protocols4 min
- Assign n tasks to n workers at minimum cost, with n up to 20. Why a bitmask, and what does it cost?hardSame kind of round: coding4 min