12.17. OOP Inheritance Patterns

12.17.1. Rationale

single inheritance

One class inherits from one other class. Has one parent.

multilevel inheritance

One class inherits from other class, and yet another class inherits from it. This creates hierarchical structure.

multiple inheritance
mixin classes

One class derives from several other classes at once.

12.17.2. No Inheritance

>>> class Parent:
...     pass
>>>
>>>
>>> class Child:
...     pass

12.17.3. Single Inheritance

>>> class Parent:
...     pass
>>>
>>>
>>> class Child(Parent):
...     pass

12.17.4. Multilevel Inheritance

>>> class Person:
...     pass
>>>
>>>
>>> class Parent(Person):
...     pass
>>>
>>>
>>> class Child(Parent):
...     pass

12.17.5. Multiple Inheritance

>>> class Mother:
...     pass
>>>
>>>
>>> class Father:
...     pass
>>>
>>>
>>> class Child(Mother, Father):
...     pass

12.17.6. Assignments

Code 12.26. Solution
"""
* Assignment: OOP InheritancePatterns Multilevel
* Complexity: easy
* Lines of code: 8 lines
* Time: 3 min

English:
    1. Create class `MarsMission` from classes `Habitat`, `Rocket`, `Astronaut`
    2. Use multilevel inheritance
    3. Assignment demonstrates syntax, so do not add any attributes and methods
    4. Run doctests - all must succeed

Polish:
    1. Stwórz klasę `MarsMission` z klas `Habitat`, `Rocket`, `Astronaut`
    2. Użyj wielopoziomowego dziedziczenia
    3. Zadanie demonstruje składnię, nie dodawaj żadnych atrybutów i metod
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(Habitat)
    >>> assert isclass(Astronaut)
    >>> assert isclass(Rocket)
    >>> assert isclass(MarsMission)
    >>> assert issubclass(MarsMission, Habitat)
    >>> assert issubclass(MarsMission, Astronaut)
    >>> assert issubclass(MarsMission, Rocket)

    >>> assert len(Habitat.__subclasses__()) == 1
    >>> assert len(Astronaut.__subclasses__()) == 1
    >>> assert len(Rocket.__subclasses__()) == 1
    >>> assert len(MarsMission.__subclasses__()) == 0
"""


Code 12.27. Solution
"""
* Assignment: OOP InheritancePatterns Mixin
* Complexity: easy
* Lines of code: 8 lines
* Time: 3 min

English:
    1. Create class `MarsMission` from classes `Habitat`, `Rocket`, `Astronaut`
    2. Use mixins classes
    3. You can modify given classes
    4. Assignment demonstrates syntax, so do not add any attributes and methods
    5. Run doctests - all must succeed

Polish:
    1. Stwórz klasę `MarsMission` z klas `Habitat`, `Rocket`, `Astronaut`
    2. Użyj klas domieszkowych (mixin)
    3. Możesz modyfikować dane klasy
    4. Zadanie demonstruje składnię, nie dodawaj żadnych atrybutów i metod
    5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(Habitat)
    >>> assert isclass(Astronaut)
    >>> assert isclass(Rocket)
    >>> assert isclass(MarsMission)
    >>> assert issubclass(MarsMission, Habitat)
    >>> assert issubclass(MarsMission, Astronaut)
    >>> assert issubclass(MarsMission, Rocket)

    >>> assert len(Habitat.__subclasses__()) == 1
    >>> assert len(Astronaut.__subclasses__()) == 1
    >>> assert len(Rocket.__subclasses__()) == 1
    >>> assert len(MarsMission.__subclasses__()) == 0
"""