4.1. Inheritance None
4.1.1. Example
>>> class Account:
... pass
>>>
>>> class User:
... pass
>>>
>>> class Admin:
... pass
4.1.2. Diagram
4.1.3. Code
class User:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
class Staff:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
def astuple(self):
data = vars(self).values()
return tuple(data)
class Admin:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
def astuple(self):
data = vars(self).values()
return tuple(data)
def asdict(self):
data = vars(self)
return dict(data)
4.1.4. 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 None A
# - Difficulty: easy
# - Lines: 6
# - Minutes: 3
# %% 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ść
# %% 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(User)
>>> assert isclass(Admin)
>>> 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
"""
# Define class `Account`
# Define class `User`
# Define class `Admin`
# Do not use inheritance
# type: type[Account]
...