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 = engineRelated terms
- inheritance — Declaring one class or type as a specialised version of another, so it receives the parent's members and can be used wherever the parent is expected.
- class — A blueprint in code that bundles data and the functions that work on it, from which objects are made.
Taught in
- How A Web Page Reaches You — Hands-on Python: Complete Video Course & Book
- Inheritance And Composition — Hands-on Python: Complete Video Course & Book