17.2. OOP Class
Classes are templates for objects (instances)
Classes use
PascalCasename convention
- class
Templates for objects.
Syntax:
class <name>:
...
Example:
>>> class User:
... pass
17.2.1. Class
Classes are templates for objects
Classes should have capitalized name:
>>> class User:
... pass
17.2.2. Naming
PascalCasename conventionEach word should start with uppercase letter
Other letter lowercased
Do not use
_to join wordsBuiltin classes use all lowercase letters
>>> class User:
... pass
>>> class SuperUser:
... pass
17.2.3. Builtin Classes
Builtin classes use all lowercase letters:
>>> class int:
... pass
>>>
>>> class float:
... pass
>>>
>>> class bool:
... pass
>>>
>>> class str:
... pass
>>>
>>> class tuple:
... pass
>>>
>>> class list:
... pass
>>>
>>> class set:
... pass
>>>
>>> class dict:
... pass
Yes, it means that you have always used object oriented programming without even knowing it!
17.2.4. Convention
When classes are short, you can put several classes in one file
When classes are long, put one class per file
You can mix classes and functions in one file
You can mix classes and functions in one file:
>>> class User:
... pass
>>>
>>> class SuperUser:
... pass
>>>
>>> def main():
... pass
>>>
>>> data = 1
17.2.5. Recap
Classes are templates for objects (instances)
Classes use
PascalCasename conventionWhen classes are short, you can put several classes in one file
When classes are long, put one class per file
You can mix classes and functions in one file
>>> class User:
... pass
>>>
>>> class SuperUser:
... pass
>>>
>>> def main():
... pass
>>>
>>> data = 1
17.2.6. Use Case - 1
>>> class Account:
... pass
>>>
>>>
>>> class User:
... pass
>>>
>>>
>>> class Admin:
... pass
17.2.7. Assignments
# %% About
# - Name: OOP Class One
# - Difficulty: easy
# - Lines: 2
# - Minutes: 1
# %% 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. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj klasę `User`
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> type(User)
# <class 'type'>
# %% 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`.'
>>> type(User)
<class 'type'>
"""
# %% 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
User: type
# %% Data
# %% Result
# %% About
# - Name: OOP Class Many
# - Difficulty: easy
# - Lines: 4
# - Minutes: 1
# %% 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. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj klasę `User`
# 2. Zdefiniuj klasę `Admin`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> type(User)
# <class 'type'>
#
# >>> type(Admin)
# <class 'type'>
# %% 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 isclass(Admin), \
'Object `Admin` has an invalid type; expected: `class`.'
>>> type(User)
<class 'type'>
>>> type(Admin)
<class 'type'>
"""
# %% 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
User: type
Admin: type
# %% Data
# %% Result
# %% About
# - Name: OOP Class Naming
# - Difficulty: easy
# - Lines: 4
# - Minutes: 1
# %% 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 `SuperUser`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj klasę `User`
# 2. Zdefiniuj klasę `SuperUser`
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> type(User)
# <class 'type'>
#
# >>> type(SuperUser)
# <class 'type'>
# %% 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 isclass(SuperUser), \
'Object `SuperUser` has an invalid type; expected: `class`.'
>>> type(User)
<class 'type'>
>>> type(SuperUser)
<class 'type'>
"""
# %% 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
User: type
SuperUser: type
# %% Data
# %% Result