Skip to content
Tech Interview Prep
Technical interview guide

OOP & Data Classes

Classes, inheritance, and the @dataclass shortcut for the common case of a class that's mostly just data.

Minutes to read
3
Practice MCQs
25
Interview Q&A
25
Edition
v3
Editorial status
Reviewed

Scope: Python 3.14 object model, dataclasses, typing.Protocol, and abstract base classes.

Overview

Curated: · Written: · Reviewed:

OOP & Data Classes

In one line

Python resolves methods, descriptors and special methods through the type, using a C3 MRO for multiple inheritance. Reach for @dataclass when a class is mostly fields and equality comes from those fields; write a plain class when identity or custom behaviour dominates.

Key definitions

  • Bound method: a function found on the type, wrapped by its __get__ with the instance as self.
  • MRO: the C3 linearization of a class; super() continues along type(self).__mro__, not to a named parent.
  • Descriptor: an object defining __get__ (and __set__ for data descriptors) on a class. Methods and property are descriptors.
  • Special method: a dunder such as __len__ or __eq__ looked up on type(obj), so instance attributes do not affect len(), == or hash().
  • InitVar / ClassVar: an init-only parameter passed to __post_init__, and a class-level name excluded from fields.
  • Protocol vs ABC: a structural static shape versus a nominal contract with register() and abstract methods.

How it works

  • obj.m finds m on type(obj) through the MRO, calls its __get__, and returns a bound method holding obj.
  • Attribute reads check data descriptors on the type, then the instance __dict__, then class attributes and non-data descriptors.
  • @dataclass writes __init__, __repr__ and __eq__ into the class at creation time. A method you define yourself suppresses the generated one.
  • In a diamond, D().close() walks D, B, C, A, object. B does not shadow C because C3 keeps local order and places A last.
D B C A object

When to use and when not to

SituationUseAvoid
Mostly data, equality from fields@dataclassHand-written value class with drifting __eq__
Hashable keyfrozen=True with immutable fieldsunsafe_hash=True on mutable state
Many stable instancesslots=True, after measuringSubclasses that re-add __dict__
Shared behaviour, unrelated typesProtocolABC inheritance forced on third-party types
Runtime nominal check, shared codeABCProtocol, when isinstance must mean subclass
Reuse without substitutabilityCompositionInheritance that exposes base internals

Traps interviewers probe

  1. Shared class-level mutables. items = [] in the class body makes one list shared by every instance, and a.items.append mutates it. Fix: create the list in __init__ or use field(default_factory=list).
  2. Hard-coded class in a classmethod. return Query(...) inside count ignores subclasses. Fix: return cls(...).
  3. Hashing mutable state. Defining __eq__ sets __hash__ to None. Forcing a hash with unsafe_hash=True and then mutating a field makes the dict lookup miss, giving KeyError. Fix: frozen dataclass with identity fields.
  4. Patching dunders on an instance. w.__len__ = ... changes w.__len__() but not len(w), because implicit lookup uses the type. Fix: define the protocol method on the class.
  5. Overclaiming frozen. frozen=True blocks attribute assignment only. cfg.tags += [...] mutates the list in place before FrozenInstanceError is raised. Fix: tuple or frozenset fields.

Interview tips

Opening line: "Special methods and descriptors resolve on the type, so the class decides behaviour and the instance only holds state. A dataclass is code generation on top of that."

Expected follow-ups:

  • Why D().close() returns the method from C in a diamond, and how cooperative super() chains must accept **kw.
  • Why defining __eq__ removes hashing, and what a frozen dataclass hashes.
  • What __slots__ saves, and why a subclass without slots gives the memory back.
  • Protocol vs ABC for a library extension point, and why register() adds no methods.
  • Why replace() reruns __post_init__, and why reordering fields breaks case Point(a, b).
  • __init_subclass__ and __set_name__ before reaching for a metaclass.

30-second outline:

  1. Definition: the class holds state, the type owns behaviour, and @dataclass generates the boilerplate.
  2. Mechanism: name the lookup or MRO rule and show one example, such as D, B, C, A, object.
  3. Trade-off and fix: name the trap (shared mutable, unsafe hash, slots compatibility) and the change that avoids it.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.