16.18. OOP Inheritance About

  • 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.

16.18.1. One Child

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

16.18.2. Many Children

>>> class Account:
...     pass
>>>
>>>
>>> class User(Account):
...     pass
>>>
>>> class Admin(Account):
...     pass

16.18.3. Use Case - 0x01

>>> class Iris:
...     pass
>>>
>>>
>>> class Setosa(Iris):
...     pass
>>>
>>> class Versicolor(Iris):
...     pass
>>>
>>> class Virginica(Iris):
...     pass

16.18.4. Assignments

"""
* Assignment: OOP Inheritance None
* Type: class assignment
* Complexity: easy
* Lines of code: 6 lines
* Time: 2 min

English:
    1. Define class `Account` inheriting from object
    2. Define class `User` inheriting from object
    3. Define class `Admin` inheriting from object
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `Account` dziedziczącą po object
    2. Zdefiniuj klasę `User` dziedziczącą po object
    3. Zdefiniuj klasę `Admin` dziedziczącą po object
    4. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Account)
    >>> assert issubclass(Account, object)
    >>> assert issubclass(Account, Account)
    >>> assert not issubclass(Account, User)
    >>> assert not issubclass(Account, Admin)

    >>> assert isclass(User)
    >>> assert issubclass(User, object)
    >>> assert not issubclass(User, Account)
    >>> assert issubclass(User, User)
    >>> assert not issubclass(User, Admin)

    >>> assert isclass(Admin)
    >>> assert issubclass(Admin, object)
    >>> assert not issubclass(Admin, Account)
    >>> assert not issubclass(Admin, User)
    >>> assert issubclass(Admin, Admin)
"""

# Define class `Account` inheriting from object
...

# Define class `User` inheriting from object
...

# Define class `Admin` inheriting from object
...


"""
* Assignment: OOP Inheritance Single
* Type: class assignment
* Complexity: easy
* Lines of code: 6 lines
* Time: 2 min

English:
    1. Define class `Account` inheriting from object
    2. Define class `User` inheriting from `Account`
    3. Define class `Admin` inheriting from `Account`
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `Account` dziedziczącą po object
    2. Zdefiniuj klasę `User` inheriting from `Account`
    3. Zdefiniuj klasę `Admin` inheriting from `Account`
    4. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Account)
    >>> assert issubclass(Account, object)
    >>> assert issubclass(Account, Account)
    >>> assert not issubclass(Account, User)
    >>> assert not issubclass(Account, Admin)

    >>> assert isclass(User)
    >>> assert issubclass(User, object)
    >>> assert issubclass(User, Account)
    >>> assert issubclass(User, User)
    >>> assert not issubclass(User, Admin)

    >>> assert isclass(Admin)
    >>> assert issubclass(Admin, object)
    >>> assert issubclass(Admin, Account)
    >>> assert not issubclass(Admin, User)
    >>> assert issubclass(Admin, Admin)
"""

# Define class `Account` inheriting from object
...

# Define class `User` inheriting from `Account`
...

# Define class `Admin` inheriting from `Account`
...


"""
* Assignment: OOP Inheritance Multilevel
* Type: class assignment
* Complexity: easy
* Lines of code: 6 lines
* Time: 2 min

English:
    1. Define class `Account` inheriting from object
    2. Define class `User` inheriting from `Account`
    3. Define class `Admin` inheriting from `User`
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `Account` dziedziczącą po object
    2. Zdefiniuj klasę `User` inheriting from `Account`
    3. Zdefiniuj klasę `Admin` inheriting from `User`
    4. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Account)
    >>> assert issubclass(Account, object)
    >>> assert issubclass(Account, Account)
    >>> assert not issubclass(Account, User)
    >>> assert not issubclass(Account, Admin)

    >>> assert isclass(User)
    >>> assert issubclass(User, object)
    >>> assert issubclass(User, Account)
    >>> assert issubclass(User, User)
    >>> assert not issubclass(User, Admin)

    >>> assert isclass(Admin)
    >>> assert issubclass(Admin, object)
    >>> assert issubclass(Admin, Account)
    >>> assert issubclass(Admin, User)
    >>> assert issubclass(Admin, Admin)
"""

# Define class `Account` inheriting from object
...
# Define class `User` inheriting from `Account`
...
# Define class `Admin` inheriting from `User`
...