17.9. OOP Inheritance

  • Used to avoid code duplication

  • Subclass inherits all fields and methods from baseclass

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

17.9.1. Problem

>>> class User:
...     def login(self):
...         print('ok')
>>>
>>>
>>> class Admin:
...     def login(self):
...         print('ok')
>>> alice = User()
>>> alice.login()
ok
>>>
>>> bob = Admin()
>>> bob.login()
ok

17.9.2. Solution

>>> class Account:
...     def login(self):
...         print('ok')
>>>
>>>
>>> class User(Account):
...     pass
>>>
>>> class Admin(Account):
...     pass
>>> alice = User()
>>> alice.login()
ok
>>>
>>> bob = Admin()
>>> bob.login()
ok

17.9.3. No Inheritance

  • Account inherits from object

  • User inherits from object

  • Admin inherits from object

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

17.9.4. Single Inheritance

  • Account inherits from object

  • User inherits from Account

  • Admin inherits from Account

../../_images/inheritance-patterns-single1.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

17.9.5. 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-linear1.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

17.9.6. MRO

>>> class Account:
...     pass
>>>
>>> class User(Account):
...     pass
>>>
>>> class Admin(User):
...     pass
>>> Account.mro()
[<class '__main__.Account'>, <class 'object'>]
>>>
>>> User.mro()
[<class '__main__.User'>, <class '__main__.Account'>, <class 'object'>]
>>>
>>> Admin.mro()
[<class '__main__.Admin'>, <class '__main__.User'>, <class '__main__.Account'>, <class 'object'>]

17.9.7. Everything Inherits from Object

  • Everything in Python is an object

  • All built-in types inherit from object

  • All custom types inherit from object

  • Even classes inherit from object

  • object is where the original __init__, __str__, __repr__ methods are defined

All built-in types inherit from object:

>>> int.mro()
[<class 'int'>, <class 'object'>]
>>> float.mro()
[<class 'float'>, <class 'object'>]
>>> bool.mro()
[<class 'bool'>, <class 'int'>, <class 'object'>]
>>> str.mro()
[<class 'str'>, <class 'object'>]
>>> tuple.mro()
[<class 'tuple'>, <class 'object'>]
>>> list.mro()
[<class 'list'>, <class 'object'>]
>>> set.mro()
[<class 'set'>, <class 'object'>]
>>> dict.mro()
[<class 'dict'>, <class 'object'>]

17.9.8. Use Case - 1

>>> bool.mro()
[<class 'bool'>, <class 'int'>, <class 'object'>]
>>>
>>> int.mro()
[<class 'int'>, <class 'object'>]
>>> type(True)
<class 'bool'>
>>>
>>> type(1)
<class 'int'>
>>> isinstance(True, bool)
True
>>>
>>> isinstance(True, int)
True
>>>
>>> isinstance(True, object)
True
>>> isinstance(1, bool)
False
>>>
>>> isinstance(1, int)
True
>>>
>>> isinstance(1, object)
True

17.9.9. Use Case - 2

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

17.9.10. References

17.9.11. Assignments

# %% About
# - Name: OOP Inheritance 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ść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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)
"""

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

# %% Imports

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

# %% Data

# %% Result

# %% About
# - Name: OOP Inheritance 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ść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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)
"""

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

# %% Imports

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

# %% Data

# %% Result

# %% About
# - Name: OOP Inheritance 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ść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> 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)
"""

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

# %% Imports

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

# %% Data

# %% Result