Dunder methods are the special methods with double underscores that Python syntax dispatches to. Calling len on an object invokes dunder len, the plus operator invokes dunder add, and a for loop invokes dunder iter. Implementing them makes your class behave like a built in type, so the language protocols work on your objects instead of you inventing custom method names.
Why interviewers ask this
This distinguishes people who write Python from people who write another language in Python syntax. The interviewer wants to hear the protocol idea: that duck typing in CPython is implemented through these hooks, and that the right instinct is to implement the protocol rather than bolt on a method called to_string. Knowing which dunder to pick, such as repr versus str, signals day to day fluency.
How to structure your answer
- Define them as hooks the syntax calls.
- Give two or three concrete mappings.
- Explain the protocol idea behind them.
- Name the ones you implement most often.
Example answer
They are the hooks the interpreter calls when you use syntax. Square bracket access goes to dunder getitem, the in keyword goes to dunder contains, equality goes to dunder eq, and so on. The point is that Python defines behavior as protocols rather than interfaces you inherit, so if my object implements dunder iter and dunder next it is an iterator, full stop, no base class required. In practice the ones I implement most are repr, eq, and hash. Repr especially, because a good repr showing the identifying fields turns a useless debugging session into a five minute one, whereas the default object at 0x7f prints tell you nothing. I am careful with eq and hash together, since defining eq without hash makes instances unhashable and they silently stop working as dict keys. On a domain model I built, adding dunder lt let us sort records with the built in sorted call instead of passing a key function everywhere.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- What is the difference between repr and str?
- Why do eq and hash have to agree?
- How does dunder getattr differ from getattribute?
Related python developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions