6.1. Inheritance About

  • no inheritance

  • single inheritance

  • linear inheritance

  • multiple inheritance (mixin classes)

single inheritance

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

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

6.1.1. No Inheritance

  • Account inherits from object

  • User inherits from object

  • Admin inherits from object

../../_images/inheritance-patterns-none.png
>>> class Account:
...     pass
>>>
>>> class User:
...     pass
>>>
>>> class Admin:
...     pass
>>> alice = Admin()
>>>
>>> isinstance(alice, Account)
False
>>>
>>> isinstance(alice, User)
False
>>>
>>> isinstance(alice, Admin)
True

6.1.2. Single Inheritance

  • Account inherits from object

  • User inherits from Account

  • Admin inherits from Account

../../_images/inheritance-patterns-single.png
>>> class Account:
...     pass
>>>
>>> class User(Account):
...     pass
>>>
>>> class Admin(Account):
...     pass
>>> alice = Admin()
>>>
>>> isinstance(alice, Account)
True
>>>
>>> isinstance(alice, User)
False
>>>
>>> isinstance(alice, Admin)
True

6.1.3. Linear Inheritance

  • Account inherits from object

  • User inherits from Account

  • Admin inherits from User

  • alice is and instance of Admin

  • alice is also an instance of User (because Admin inherits from User)

  • alice is also an instance of Account (because Admin inherits from User which inherits from Account)

../../_images/inheritance-patterns-linear.png
>>> class Account:
...     pass
>>>
>>>
>>> class User(Account):
...     pass
>>>
>>> class Admin(User):
...     pass
>>> alice = Admin()
>>>
>>> isinstance(alice, Account)
True
>>>
>>> isinstance(alice, User)
True
>>>
>>> isinstance(alice, Admin)
True

6.1.4. Multiple Inheritance

  • Account inherits from User and Admin

../../_images/inheritance-patterns-multiple.png
>>> class User:
...     pass
>>>
>>> class Admin:
...     pass
>>>
>>> class Account(User, Admin):
...     pass
>>> alice = Account()
>>>
>>> isinstance(alice, Account)
True
>>>
>>> isinstance(alice, User)
True
>>>
>>> isinstance(alice, Admin)
True

6.1.5. Assignments

# %% About
# - Name: Inheritance About None
# - Difficulty: easy
# - Lines: 6
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% 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ść

# %% Expected
# TODO: Write expected result

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from inspect import isclass

>>> assert isclass(Account), \
'Object `Account` has an invalid type; expected: `class`.'

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

>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'

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

>>> assert isclass(Admin), \
'Object `Admin` has an invalid type; expected: `class`.'

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

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
Account: type
User: type
Admin: type

# %% Data

# %% Result

# %% About
# - Name: Inheritance About Single
# - Difficulty: easy
# - Lines: 6
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% 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ść

# %% Expected
# TODO: Write expected result

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from inspect import isclass

>>> assert isclass(Account), \
'Object `Account` has an invalid type; expected: `class`.'

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

>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'

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

>>> assert isclass(Admin), \
'Object `Admin` has an invalid type; expected: `class`.'

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

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
Account: type
User: type
Admin: type

# %% Data

# %% Result

# %% About
# - Name: Inheritance About Linear
# - Difficulty: easy
# - Lines: 6
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% 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ść

# %% Expected
# TODO: Write expected result

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from inspect import isclass

>>> assert isclass(Account), \
'Object `Account` has an invalid type; expected: `class`.'

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

>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'

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

>>> assert isclass(Admin), \
'Object `Admin` has an invalid type; expected: `class`.'

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

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
Account: type
User: type
Admin: type

# %% Data

# %% Result

# %% About
# - Name: Inheritance About Multiple
# - Difficulty: easy
# - Lines: 6
# - Minutes: 2

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

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

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

# %% Expected
# TODO: Write expected result

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> from inspect import isclass

>>> assert isclass(Account), \
'Object `Account` has an invalid type; expected: `class`.'

>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'

>>> assert isclass(Admin), \
'Object `Admin` has an invalid type; expected: `class`.'

>>> assert issubclass(Account, Account)
>>> assert issubclass(Account, User)
>>> assert issubclass(Account, Admin)

>>> assert len(Account.__subclasses__()) == 0
>>> assert len(User.__subclasses__()) == 1
>>> assert len(Admin.__subclasses__()) == 1
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports

# %% Types
Account: type
User: type
Admin: type

# %% Data

# %% Result