🐍 Python Advanced

What is Python's __dunder__ (magic) methods?

Why Interviewers Ask This

Advanced questions like this reveal whether a candidate has internalized Python deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.

Answer

Python's dunder (double underscore) methods enable operator overloading and customization of built-in behaviours. Numeric: __add__ (+), __sub__ (-), __mul__ (*), __truediv__ (/), __floordiv__ (//), __mod__ (%), __pow__ (**), plus reflected (__radd__) and in-place (__iadd__) versions. Comparison: __eq__, __ne__, __lt__, __gt__, __le__, __ge__. Container: __len__, __getitem__, __setitem__, __delitem__, __contains__, __iter__, __next__. Context manager: __enter__, __exit__. Callable: __call__ — makes instances callable. Attribute: __getattr__ (called when attribute not found), __getattribute__ (called for every access). Hash: __hash__ (required for dict keys and set elements).

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Python project, I used this when...' immediately makes your answer more credible and memorable.