Technical Mid Level

Explain Python's data model and dunder methods. How would you make a custom class work with len(), iteration, and comparison operators?

Quick Tip

Give a concrete example: "My PagedResults class implements __len__ for total count, __iter__ to yield items across pages, and __getitem__ for index access. Dataclasses handle __init__, __repr__, and __eq__ automatically."

What good answers include

Python's data model lets custom classes integrate with built-in operations by implementing special methods: __len__ for len(), __iter__/__next__ for iteration, __eq__/__lt__/__hash__ for comparison and hashing, __repr__/__str__ for string representation, __getitem__ for indexing, and __enter__/__exit__ for context managers. Strong candidates discuss the total_ordering decorator, the difference between __repr__ and __str__, and dataclasses as a shortcut for common dunder methods.

What interviewers are looking for

Tests Python depth beyond surface usage. Candidates who cannot implement basic dunder methods will struggle with Pythonic design. Ask them to sketch a class that works as a context manager — this tests __enter__/__exit__ understanding.

← All Python questions