16.2. OOP Class

  • Object Oriented Paradigm

  • Model world as objects that interacts with each other

  • Classes are templates for objects

  • PascalCase name convention

  • Never print in a class

class

Templates for objects.

Syntax:

class <name>:
    ...

Example:

>>> class User:
...     pass

16.2.1. Class

  • Classes are templates for objects

Classes should have capitalized name:

>>> class User:
...     pass

16.2.2. Naming

  • PascalCase name convention

  • Each word should start with uppercase letter

  • Other letter lowercased

  • Do not use _ to join words

  • Builtin classes use all lowercase letters

>>> class User:
...     pass
>>> class SuperUser:
...     pass

16.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!

16.2.4. Convention

  • Never print in a class

  • All classes in one file - when classes are short

  • One class per file - when classes are long

You can mix classes and functions in one file:

>>> class User:
...     pass
>>>
>>> def main():
...     pass
>>>
>>> name = 'Mark'

16.2.5. Use Case - 1

>>> class Account:
...     pass
>>>
>>>
>>> class User:
...     pass
>>>
>>>
>>> class Admin:
...     pass

16.2.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: OOP Class One
# - Difficulty: easy
# - Lines: 2
# - Minutes: 2

# %% English
# 1. Define class `User`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj klasę `User`
# 2. 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(User)
"""

# Define class `User`
# type: type
...


# %% 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: OOP Class Many
# - Difficulty: easy
# - Lines: 4
# - Minutes: 2

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

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

>>> from inspect import isclass

>>> assert isclass(User)
>>> assert isclass(Admin)
"""

# Define class `User`
# type: type
...

# Define class `Admin`
# type: type
...