Python Object-Oriented Programming
Chris Roda · 2024
Code re-use is a theoretical objective all programmers must strive to achieve. Python Provides two strategies for re-using object-oriented class structure: Inheritance and Composition. Inheritance exhibits “is a” relationships and answers the question, “Is class B a type of class A?” Inheritance derives subclasses from base classes. Derived classes inherit all attributes and methods from their base classes. The relationship between derived classes and their base classes is called “tightly coupled”. Changes in base classes have profound impact on derived classes. Derived subclasses extend their original base class functionality by altering, appending to or entirely rewriting base class functionality. Composition exhibits “has a” relationships and answers the question, “Does class A have components of class B?” Composition classes are composites of pre-existing component classes. This relationship is described as, “loosely coupled”. Proper planning and understanding are required for choosing between inheritance and composition.