16.3. OOP Instance
Definition: instance - object created from class
Instances are objects
Convention:
snake_case
namesConvention: Two newlines between class and instances
16.3.1. One Class, One Instance
One class and one instance:
>>> class User:
... pass
>>>
>>>
>>> mark = User()
16.3.2. One Class, Many Instances
One class and three instances:
>>> class User:
... pass
>>>
>>>
>>> mark = User()
>>> melissa = User()
>>> rick = User()
16.3.3. Many Classes, Many Instances
Two classes and six instances. One instance of an Admin
class, and
five instances of User
class:
>>> class User:
... pass
...
>>> class Admin:
... pass
>>>
>>>
>>> melissa = Admin()
>>> mark = User()
>>> rick = User()
>>> alex = User()
>>> beth = User()
>>> chris = User()
16.3.4. Naming
>>> class User:
... pass
>>>
>>>
>>> mark = User()
>>> markwatney = User()
>>> mark_watney = User()
16.3.5. Type
Builtin
type()
returns type (class) of an objecttype(obj)
type(obj) is cls
>>> class User:
... pass
>>>
>>> class Admin:
... pass
>>>
>>>
>>> mark = User()
>>>
>>> type(mark)
<class '__main__.User'>
>>>
>>> type(mark) is User
True
>>>
>>> type(mark) is Admin
False
16.3.6. Isinstance
isinstance()
- returns if object is instance of classisinstance(mark, User)
isinstance(mark, Admin)
isinstance(mark, User|Admin)
>>> class User:
... pass
>>>
>>> class Admin:
... pass
>>>
>>>
>>> mark = User()
>>>
>>> isinstance(mark, User)
True
>>>
>>> isinstance(mark, Admin)
False
>>>
>>> isinstance(mark, User|Admin)
True
16.3.7. Instances of Builtin Classes
>>> a = int()
>>> b = float()
>>> c = bool()
>>> d = str()
>>> e = list()
>>> f = tuple()
>>> g = set()
>>> h = dict()
16.3.8. Use Case - 1
>>> x = list()
>>> y = list()
The above works because someone created class list()
:
>>> class List:
... pass
>>>
>>>
>>> x = List()
>>> y = List()
16.3.9. Use Case - 2
>>> x = list()
>>> y = tuple()
The above works because someone created classes list()
and tuple()
:
>>> class List:
... pass
>>>
>>>
>>> class Tuple:
... pass
>>>
>>>
>>> x = List()
>>> y = Tuple()
16.3.10. Use Case - 4
>>> class Astronaut:
... pass
>>>
>>>
>>> mark = Astronaut()
>>> melissa = Astronaut()
>>> rick = Astronaut()
>>> alex = Astronaut()
>>> beth = Astronaut()
>>> chris = Astronaut()
16.3.11. Use Case - 5
>>> class AstronautPilot:
... pass
...
>>> class AstronautScientist:
... pass
...
>>> class AstronautEngineer:
... pass
...
>>> class AstronautPhysician:
... pass
>>>
>>>
>>> mark_watney = AstronautScientist()
>>> melissa_lewis = AstronautEngineer()
>>> rick_martinez = AstronautPilot()
>>> alex_vogel = AstronautScientist()
>>> beth_johanssen = AstronautEngineer()
>>> chris_beck = AstronautPhysician()
16.3.12. References
16.3.13. 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 Instance One
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 1. Create instance `mark` of a class `User`
# 1. Run doctests - all must succeed
# %% Polish
# 1. Stwórz instancję `mark` klasy `User`
# 1. 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 isinstance(mark, User)
"""
class User:
pass
# Create instance `mark` of a class `User`
# type: User
...
# %% 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 Instance Many
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Create instance `mark` of a class `User`
# 2. Create instance `melissa` of a class `Admin`
# 3. Run doctests - all must succeed
# %% Polish
# 1. Stwórz instancję `mark` klasy `User`
# 2. Stwórz instancję `melissa` klasy `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)
>>> assert isinstance(mark, User)
>>> assert isinstance(melissa, Admin)
"""
class User:
pass
class Admin:
pass
# Create instance `mark` of a class `User`
# type: User
...
# Create instance `melissa` of a class `Admin`
# type: Admin
...
# %% 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 Instance IsInstance
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Define `result: type` with a result of checking a type of `mark`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: type` z wynikiem sprawdzania typu `marka`
# 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)
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is type, \
'Variable `result` has invalid type, should be type'
>>> print(result) # doctest: +ELLIPSIS
<class '....User'>
"""
class User:
pass
mark = User()
# Define `result: type` with a result of checking a type of `mark`
# type: type[User]
result = ...
# %% 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 Instance IsInstance
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Define `result: bool` with a result of checking
# if `mark` is and instance of a class `User`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bool` z wynikiem sprawdzania,
# czy `mark` jest instancją klasy `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)
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is bool, \
'Variable `result` has invalid type, should be bool'
>>> print(result)
True
"""
class User:
pass
mark = User()
# Define `result: bool` with a result of checking
# if `mark` is and instance of a class `User`
# type: bool
result = ...
# %% 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 Instance IsInstance
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% English
# 1. Define `result: bool` with a result of checking
# if `mark` is and instance of a class `User` or `Admin`
# 2. Run doctests - all must succeed
# %% Polish
# 1. Zdefiniuj `result: bool` z wynikiem sprawdzania,
# czy `mark` jest instancją klasy `User` lub `Admin`
# 2. Uruchom doctesty - wszystkie muszą się powieść
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
>>> from inspect import isclass
>>> assert isclass(User)
>>> assert isclass(Admin)
>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is bool, \
'Variable `result` has invalid type, should be bool'
>>> print(result)
True
"""
class User:
pass
class Admin:
pass
mark = User()
# Define `result: bool` with a result of checking
# if `mark` is and instance of a class `User` or `Admin`
# type: bool
result = ...