Questions
16 of 19
1How would you make a custom class usable in a for loop?
2How do __enter__ and __exit__ work together to implement the context manager protocol?
3How does Python implement inheritance, and what is the Method Resolution Order (MRO)?
4What are dataclasses, and how do they reduce boilerplate compared to manually writing __init__, __eq__, and __repr__?
5What is the @property decorator, and what problem does it solve compared to plain getter/setter methods?
6What is the difference between method overriding and method overloading, and why doesn't Python natively support overloading?
7What problem does the diamond inheritance pattern cause, and how does Python's MRO resolve it?
8What is the difference between a class and an instance in Python?
9How does Python implement encapsulation given that it has no true 'private' access modifiers?
10What is the purpose of self in instance methods, and why must it be explicitly declared in Python?
11What is the difference between __str__ and __repr__, and what convention should __repr__ follow?
12What is a metaclass, and what is type's relationship to every class in Python?
13What is the purpose of __eq__ and __hash__, and why must they be implemented consistently?
14What is a descriptor, and how do @property and ORMs like Django's models rely on the descriptor protocol?
15What is the difference between an instance method, a class method, and a static method?
16What is composition, and why is 'favor composition over inheritance' often recommended?
17What are dunder (magic) methods, and how do they enable operator overloading?
18When would you use a metaclass instead of a class decorator or __init_subclass__?
19How do abstract base classes (abc.ABC) enforce interface contracts in Python?
16 / 19

What is composition, and why is 'favor composition over inheritance' often recommended?

Difficulty: 8/10
Composition, Inheritance, Design Principles

Composition builds behavior by holding collaborators; it avoids fragile base-class coupling

Composition means an object holds references to other objects and delegates work to them. Inheritance models an is-a relationship and hardcodes the implementation choice at class definition time; composition models has-a or uses-a and lets you swap behavior at runtime. The classic failure of inheritance is the fragile base class problem: a change in a parent can break subclasses, and deep hierarchies become hard to reason about. Composition keeps interfaces narrow, makes mocking trivial, and keeps the class open for extension without touching the base.

  1. 1

    Inheritance is appropriate for genuine is-a relationships with stable contracts, or for abstract interfaces.

  2. 2

    Composition is appropriate for behavior reuse, strategy selection, and anything you may want to swap per instance or per test.

  3. 3

    Trade-off: composition adds delegation boilerplate. Mitigate with explicit interfaces, getattr forwarding, or plain attributes.

  4. 4

    Common mistake: using inheritance only to reuse a method, producing a hierarchy that is conceptually wrong and fragile.

  5. 5

    Common mistake: overusing inheritance for cross-cutting concerns like logging or caching; mixins are inheritance too and can make MRO brittle.

  6. 6

    Version note: the principle is language-agnostic; Python's cooperative super() and ABCs make both styles practical, so the choice is a design decision, not a language limitation.

javascript

Scenario Questions

0-2 years experience

  1. 1You need a Logger class that can write to console or file. Inheritance or composition?
  2. 2What relationship does is-a describe and what relationship does has-a describe?

2-5 years experience

  1. 1A deep hierarchy breaks when a parent changes a helper method. How do you refactor toward composition?
  2. 2You need to swap a pricing strategy per order. How do you model it?

5-8 years experience

  1. 1You are designing a service layer where caching, retries, and metrics can be enabled independently. How do you compose them?
  2. 2Your mixin-based framework has fragile MRO interactions. What does the composition alternative look like?

8+ years experience

  1. 1Design a plugin system where behavior is composed at runtime and each plugin can be tested in isolation with fakes.
  2. 2Compare composition and inheritance on testability, deployment flexibility, and performance in a large service codebase.

Follow-up Questions

  • When is inheritance genuinely the better choice than composition?
  • How does dependency injection relate to favoring composition?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.