12.15. OOP Inheritance About

12.15.1. Rationale

  • Child inherits all fields and methods from parent

  • Used to avoid code duplication

parent
superclass
base class

Class from other classes inherits

child
subclass

Class which inherits from parent

inherit
derive

Class takes attributes and methods from parent.

12.15.2. Inheritance

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

12.15.3. Methods

>>> class Parent:
...     def say_hello(self):
...         return 'Hello'
>>>
>>>
>>> class Child(Parent):
...     pass
>>>
>>>
>>> obj = Child()
>>> obj.say_hello()
'Hello'

12.15.4. Attributes

>>> class Parent:
...     def __init__(self):
...         self.firstname = 'Mark'
...         self.lastname = 'Watney'
>>>
>>>
>>> class Child(Parent):
...     pass
>>>
>>>
>>> obj = Child()
>>> vars(obj)
{'firstname': 'Mark', 'lastname': 'Watney'}

12.15.5. Use Case - 0x01

>>> class Person:
...     pass
>>>
>>>
>>> class Astronaut(Person):
...     pass
>>>
>>> class Cosmonaut(Person):
...     pass

12.15.6. Use Case - 0x02

>>> class Person:
...     def __init__(self):
...         self.firstname = 'Mark'
...         self.lastname = 'Watney'
...
...     def say_hello(self):
...         return 'hello'
>>>
>>>
>>> class Astronaut(Person):
...     pass
>>>
>>> class Cosmonaut(Person):
...     pass

12.15.7. Use Case - 0x03

>>> class Iris:
...     sepal_length: float
...     sepal_width: float
...     petal_length: float
...     petal_width: float
...     species: str
...
...     def __init__(self, sepal_length, sepal_width,
...                  petal_length, petal_width, species):
...         self.sepal_length = sepal_length
...         self.sepal_width = sepal_width
...         self.petal_length = petal_length
...         self.petal_width = petal_width
...         self.species = species
>>>
>>>
>>> class Setosa(Iris):
...     pass
>>>
>>> class Versicolor(Iris):
...     pass
>>>
>>> class Virginica(Iris):
...     pass
>>>
>>>
>>> setosa = Setosa(
...     sepal_length=5.1,
...     sepal_width=3.5,
...     petal_length=1.4,
...     petal_width=0.2,
...     species='setosa')

12.15.8. References

1

https://www.youtube.com/watch?v=EiOglTERPEo

12.15.9. Assignments

Code 12.24. Solution
"""
* Assignment: OOP Inheritance Simple
* Required: yes
* Complexity: easy
* Lines of code: 4 lines
* Time: 3 min

English:
    1. Create class `Woman` which inherits from `Venus`
    2. Create class `Man` which inherits from `Mars`
    3. Run doctests - all must succeed

Polish:
    1. Stwórz klasę `Woman`, która dziedziczy po `Venus`
    2. Stwórz klasę `Man`, która dziedziczy po `Mars`
    3. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Venus)
    >>> assert isclass(Woman)
    >>> assert isclass(Mars)
    >>> assert isclass(Man)
    >>> assert issubclass(Woman, Venus)
    >>> assert issubclass(Man, Mars)
"""


class Venus:
    pass


class Mars:
    pass