4.4. Inheritance Multiple

4.4.1. Example

>>> class UserPermissions:
...     pass
>>>
>>> class AdminPermissions:
...     pass
>>>
>>> class Account(UserPermissions, AdminPermissions):
...     pass

4.4.2. Diagram

../../_images/inheritance-usecase-multiple.png

4.4.3. Code

class Account:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname


class TupleSerializer:
    def astuple(self):
        data = vars(self).values()
        return tuple(data)


class DictSerializer:
    def asdict(self):
        data = vars(self)
        return dict(data)


class User(Account):
    pass


class Staff(Account, TupleSerializer):
    pass


class Admin(Account, TupleSerializer, DictSerializer):
    pass

4.4.4. Use Case - 1

>>> class Account:
...     def login(self): ...
...     def logout(self): ...
>>>
>>> class ManagesUsers:
...     def user_add(self): ...
...     def user_edit(self): ...
...     def user_delete(self): ...
>>>
>>> class ManagesStaff:
...     def staff_add(self): ...
...     def staff_edit(self): ...
...     def staff_delete(self): ...
>>>
>>>
>>> class User(Account):
...     pass
>>>
>>> class Staff(Account, ManagesUsers):
...     pass
>>>
>>> class Admin(Account, ManagesUsers, ManagesStaff):
...     pass

4.4.5. Use Case - 2

>>> class UserPermissions:
...     pass
>>>
>>> class AdminPermissions:
...     pass
>>>
>>> class MyAccount(UserPermissions, AdminPermissions):
...     pass

4.4.6. Assignments

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

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

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

# %% English
# 1. Define class `UserPermissions`
# 2. Define class `AdminPermissions`
# 3. Compose `Account` from `UserPermissions`, `AdminPermissions`
# 4. Use mixins classes
# 5. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj klasę `UserPermissions`
# 2. Zdefiniuj klasę `AdminPermissions`
# 3. Skomponuj `Account` z klas `UserPermissions`, `AdminPermissions`
# 4. Użyj klas domieszkowych (mixin)
# 5. Uruchom doctesty - wszystkie muszą się powieść

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

>>> from inspect import isclass

>>> assert isclass(Account)
>>> assert isclass(UserPermissions)
>>> assert isclass(AdminPermissions)

>>> assert issubclass(Account, Account)
>>> assert issubclass(Account, UserPermissions)
>>> assert issubclass(Account, AdminPermissions)

>>> assert len(Account.__subclasses__()) == 0
>>> assert len(UserPermissions.__subclasses__()) == 1
>>> assert len(AdminPermissions.__subclasses__()) == 1
"""

# Define class `UserPermissions`
# Define class `AdminPermissions`
# Compose `Account` from `UserPermissions`, `AdminPermissions`
# Use mixins classes
# type: type[Account]
...