Follow the transformation
Define the class and decide which state belongs to each instance.
Initialise required state in __init__ or use a dataclass for data-focused containers.
Use instance methods for behaviour that depends on object state.
Composition Over Inheritance is part of object-oriented Python.
Composition Over Inheritance is part of object-oriented Python. A class defines behaviour and structure for objects; each instance can hold its own state, and methods operate on that state through self.
Composition Over Inheritance matters because object-oriented design is useful only when state and behaviour genuinely belong together. Understanding the boundary between objects, functions and composition prevents unnecessary class complexity while enabling clear reusable abstractions.
Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Define the class and decide which state belongs to each instance. Stage 2: Initialise required state in __init__ or use a dataclass for data-focused containers. Stage 3: Use instance methods for behaviour that depends on object state. Final checkpoint: Use properties when attribute access needs validation or derived behaviour.
Define the class and decide which state belongs to each instance.
Initialise required state in __init__ or use a dataclass for data-focused containers.
Use instance methods for behaviour that depends on object state.
Define the class and decide which state belongs to each instance. This is an input-preparation stage for Composition Over Inheritance. Verify the relevant type, shape, units, keys, missingness or assumptions before later steps depend on them.
# Step 1 — Import only the named objects needed by the following steps, keeping dependencies explicit.
from dataclasses import dataclass
# Step 2 — Prepare the decorator that will modify or wrap the definition that follows.
@dataclass
class ExperimentRun:
# Step 3 — Execute this statement and inspect how it changes the current value, object or program state.
name: str
# Step 4 — Execute this statement and inspect how it changes the current value, object or program state.
score: float
# Step 5 — Define the reusable `passed` function; its indented body describes what happens for each call.
def passed(self, threshold=0.8):
# Step 6 — Return the computed value to the caller so the result can be reused or tested.
return self.score >= threshold
# Step 7 — Compute the right-hand expression and store its result in `run` for the next step.
run = ExperimentRun("baseline", 0.84)
# Step 8 — Display the current value explicitly so the result/state can be inspected during execution.
print(run)
# Step 9 — Display the current value explicitly so the result/state can be inspected during execution.
print(run.passed())ExperimentRun(name='baseline', score=0.84) True
For Composition Over Inheritance, connect the displayed result to the specific input and mechanism above; independently verify one value/state change rather than treating successful execution as proof.
classBlueprint/name that defines attributes and methods.instanceConcrete object created from a class.inheritanceDerive behaviour from a parent class when the subtype relationship is genuine.compositionBuild an object from other collaborating objects; usually more flexible.Use Composition Over Inheritance when the program genuinely needs this language behaviour and you can state the input object, resulting value/state and expected failure behaviour.
Choose a clearer built-in, data structure or control-flow pattern when it expresses the intent more directly; stop if implicit conversion, mutation or hidden state makes the behaviour hard to reason about.
Build a tiny, inspectable example of Composition Over Inheritance. First define the class and decide which state belongs to each instance. Then initialise required state in __init__ or use a dataclass for data-focused containers. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.
Before trusting a result from Composition Over Inheritance, which check provides the strongest evidence that you understand and applied it correctly?
Step 1Define the class and decide which state belongs to each instance.Step 2Initialise required state in __init__ or use a dataclass for data-focused containers.Step 3Use instance methods for behaviour that depends on object state.Step 4Prefer composition when one object should contain/use another behaviour rather than “be a kind of” another class.