Question presented to candidate:
"You see classes like class UserView(LoginRequiredMixin, JsonMixin, View). What is a mixin, how does Python decide which method runs when several mixins define the same one, and what makes a mixin well designed?"
What a strong answer should cover:
- A mixin is a small class that provides one capability (methods, rarely state) to be combined with other classes through multiple inheritance; it is not meant to be instantiated alone
- Order matters: mixins go to the left of the main base class, because the MRO (C3 linearisation) searches left to right; on the wrong side the base method wins
- Overriding mixins must call
super(), which means the next class in the MRO, not the parent, so several mixins chain cooperatively - A mixin relies on a contract with its host (attributes or methods it expects); state-carrying mixins accept
**kwargsand pass the rest on; empty__slots__ = ()keeps them compatible with slotted classes - Real examples:
collections.abcmixin methods,socketserver.ThreadingMixIn, Django class-based view mixins; alternatives are composition, class decorators and Protocols
Clarifying questions expected:
- Does the capability need its own state, or only methods that use the host attributes?
Code / implementation expected: Yes β two small mixins combined on one class, with the MRO printed, ideally one that overrides a method with super().