5.2. OOP Modifiers
Access modifiers control the visibility of class members
Python uses naming conventions to indicate access levels
No strict enforcement, relies on developer discipline
Common modifiers: public, protected, private
5.2.1. Public
In Python all members are public by default
>>> class User:
... def __init__(self):
... self.firstname = 'Alice'
... self.lastname = 'Apricot'
>>>
>>>
>>> myuser = User()
>>>
>>> vars(myuser)
{'firstname': 'Alice', 'lastname': 'Apricot'}
5.2.2. Protected
Python uses naming conventions to indicate access levels
No strict enforcement, relies on developer discipline
>>> class User:
... def __init__(self):
... self._firstname = 'Alice'
... self._lastname = 'Apricot'
>>>
>>>
>>> myuser = User()
>>>
>>> vars(myuser)
{'_firstname': 'Alice', '_lastname': 'Apricot'}
5.2.3. Private
Python uses naming conventions to indicate access levels
No strict enforcement, relies on developer discipline
>>> class User:
... def __init__(self):
... self.__firstname = 'Alice'
... self.__lastname = 'Apricot'
Private members are name-mangled to prevent accidental access from outside the class. The actual attribute names are changed to include the class name as a prefix.
>>> myuser = User()
>>>
>>> vars(myuser)
{'_User__firstname': 'Alice', '_User__lastname': 'Apricot'}
5.2.4. Assignments
# %% About
# - Name: OOP Access Modifiers Public
# - Difficulty: easy
# - Lines: 3
# - 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. Create class `User`, with attributes:
# - `firstname: str` (public)
# - `lastname: str` (public)
# 2. Attributes must be set at the initialization from constructor arguments
# 3. Run doctests - all must succeed
# %% Polish
# 1. Stwórz klasę `User`, z atrybutami:
# - `firstname: str` (publiczny)
# - `lastname: str` (publiczny)
# 2. Atrybuty mają być ustawiane przy inicjalizacji z parametrów konstruktora
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> alice = User('Alice', 'Apricot')
# >>> vars(alice)
# {'firstname': 'Alice', 'lastname': 'Apricot'}
# %% 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(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert hasattr(User, '__annotations__'), \
'Class `User` has an invalid attribute; expected: to have an attribute `__annotations__`.'
>>> alice = User('Alice', 'Apricot')
>>> assert 'firstname' in vars(alice)
>>> assert 'lastname' in vars(alice)
"""
# %% 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
from typing import Callable
User: type
__init__: Callable[[str, str], None]
# %% Data
# %% Result
class User:
def __init__(self):
...
# %% About
# - Name: OOP Access Modifiers Protected
# - Difficulty: easy
# - Lines: 3
# - 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. Create class `User`, with attributes:
# - `firstname: str` (protected)
# - `lastname: str` (protected)
# 2. Attributes must be set at the initialization from constructor arguments
# 3. Run doctests - all must succeed
# %% Polish
# 1. Stwórz klasę `User`, z atrybutami:
# - `firstname: str` (protected)
# - `lastname: str` (protected)
# 2. Atrybuty mają być ustawiane przy inicjalizacji z parametrów konstruktora
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> alice = User('Alice', 'Apricot')
# >>> vars(alice)
# {'_firstname': 'Alice', '_lastname': 'Apricot'}
# %% 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(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert hasattr(User, '__annotations__'), \
'Class `User` has an invalid attribute; expected: to have an attribute `__annotations__`.'
>>> alice = User('Alice', 'Apricot')
>>> assert '_firstname' in vars(alice)
>>> assert '_lastname' in vars(alice)
"""
# %% 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
from typing import Callable
User: type
__init__: Callable[[str, str], None]
# %% Data
# %% Result
class User:
def __init__(self):
...
# %% About
# - Name: OOP Access Modifiers Private
# - Difficulty: easy
# - Lines: 3
# - 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. Create class `User`, with attributes:
# - `firstname: str` (private)
# - `lastname: str` (private)
# 2. Attributes must be set at the initialization from constructor arguments
# 3. Run doctests - all must succeed
# %% Polish
# 1. Stwórz klasę `User`, z atrybutami:
# - `firstname: str` (private)
# - `lastname: str` (private)
# 2. Atrybuty mają być ustawiane przy inicjalizacji z parametrów konstruktora
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> alice = User('Alice', 'Apricot')
# >>> vars(alice)
# {'_User__firstname': 'Alice', '_User__lastname': 'Apricot'}
# %% 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(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert hasattr(User, '__annotations__'), \
'Class `User` has an invalid attribute; expected: to have an attribute `__annotations__`.'
>>> alice = User('Alice', 'Apricot')
>>> assert '_User__firstname' in vars(alice)
>>> assert '_User__lastname' in vars(alice)
"""
# %% 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
from typing import Callable
User: type
__init__: Callable[[str, str], None]
# %% Data
# %% Result
class User:
def __init__(self):
...