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
Accountinherits fromobjectUserinherits fromobjectAdmininherits fromobject
>>> 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
Accountinherits fromobjectUserinherits fromAccountAdmininherits fromAccount
>>> 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
Accountinherits fromobjectUserinherits fromAccountAdmininherits fromUseraliceis and instance ofAdminaliceis also an instance ofUser(becauseAdmininherits fromUser)aliceis also an instance ofAccount(becauseAdmininherits fromUserwhich inherits fromAccount)
>>> 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
Accountinherits fromUserandAdmin
>>> 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 None A
# - Difficulty: easy
# - Lines: 6
# - Minutes: 3
# %% 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`
# 2. Define class `User`
# 3. Define class `Admin`
# 4. Do not use inheritance
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj klasę `Account`
# 2. Zdefiniuj klasę `User`
# 3. Zdefiniuj klasę `Admin`
# 4. Nie używaj dziedziczenia
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% 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 not issubclass(User, Account)
>>> assert not issubclass(Admin, Account)
>>> assert len(Account.__subclasses__()) == 0
>>> assert len(User.__subclasses__()) == 0
>>> assert len(Admin.__subclasses__()) == 0
"""
# %% 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 Single A
# - Difficulty: easy
# - Lines: 6
# - Minutes: 3
# %% 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`
# 2. Define class `User` inheriting from `Account`
# 3. Define class `Admin` inheriting from `Account`
# 4. Use single inheritance
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj klasę `Account`
# 2. Zdefiniuj klasę `User` dziedziczącą po `Account`
# 3. Zdefiniuj klasę `Admin` dziedziczącą po `Account`
# 4. Użyj pojedynczego dziedziczenia
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% 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(User, Account)
>>> assert issubclass(Admin, Account)
>>> assert not issubclass(User, Admin)
>>> assert not issubclass(Admin, User)
"""
# %% 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 Linear A
# - Difficulty: easy
# - Lines: 6
# - Minutes: 3
# %% 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`
# 2. Define class `User` inheriting from `Account`
# 3. Define class `Admin` inheriting from `User`
# 4. Use linear inheritance
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj klasę `Account`
# 2. Zdefiniuj klasę `User` dziedziczącą po `Account`
# 3. Zdefiniuj klasę `Admin` dziedziczącą po `User`
# 4. Użyj liniowego dziedziczenia
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% 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(User, Account)
>>> assert issubclass(Admin, User)
>>> assert issubclass(Admin, Account)
>>> assert len(Account.__subclasses__()) == 1
>>> assert len(User.__subclasses__()) == 1
>>> assert len(Admin.__subclasses__()) == 0
"""
# %% 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 Multiple A
# - Difficulty: easy
# - Lines: 6
# - Minutes: 3
# %% 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`
# 2. Define class `Admin`
# 3. Define class `Account` inheriting from `User` and `Admin`
# 4. Use multiple inheritance
# 5. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj klasę `User`
# 2. Zdefiniuj klasę `Admin`
# 3. Zdefiniuj klasę `Account` dziedziczącą po `User` i `Admin`
# 4. Użyj wielo-dziedziczenia
# 5. Uruchom doctesty - wszystkie muszą się powieść
# %% 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