composition

Building a class by giving it other objects as attributes, a has-a relationship, instead of inheriting from them.

In more detail

A Car that holds an Engine in self.engine can use it without pretending to be an engine. Composition is a thinner join than inheritance, so parts can change or be swapped more easily.

Example

class Car:
    def __init__(self, engine):
        self.engine = engine

Related terms

Taught in

References