17.3. OOP Instance

  • Definition: instance - object created from class

  • Instances are objects

  • Convention: snake_case names

  • Convention: Two newlines between class and instances

instance
object

Computer software entity created from a class.

../../_images/oop-class-class.jpg

Figure 17.1. Class. Source: [1]

../../_images/oop-class-instances.jpg

Figure 17.2. Instances. Source: [2]

17.3.1. One Class, One Instance

One class and one instance:

>>> class User:
...     pass
>>>
>>>
>>> alice = User()

17.3.2. One Class, Many Instances

One class and three instances:

>>> class User:
...     pass
>>>
>>>
>>> alice = User()
>>> bob = User()
>>> carol = User()

17.3.3. Many Classes, Many Instances

Two classes and five instances. Three instances of a User class, and two instances of Admin class:

>>> class User:
...     pass
...
>>> class Admin:
...     pass
>>>
>>>
>>> alice = User()
>>> bob = User()
>>> carol = User()
>>> eve = Admin()
>>> mallory = Admin()

17.3.4. Naming

>>> class User:
...     pass
>>>
>>>
>>> alice = User()
>>> aliceapricot = User()
>>> alice_apricot = User()

17.3.5. Type

  • Builtin type() returns type (class) of an object

  • type(obj)

  • type(obj) is cls

>>> class User:
...     pass
>>>
>>> class Admin:
...     pass
>>>
>>>
>>> alice = User()

Check types:

>>> type(alice)
<class '__main__.User'>

Compare types:

>>> type(alice) is User
True
>>>
>>> type(alice) is Admin
False
>>>
>>> type(alice) in (User, Admin)
True

17.3.6. Isinstance

  • isinstance() - returns if object is instance of class

  • isinstance(alice, User)

  • isinstance(alice, Admin)

  • isinstance(alice, User|Admin)

>>> class User:
...     pass
>>>
>>> class Admin:
...     pass
>>>
>>>
>>> alice = User()

Compare types

>>> isinstance(alice, User)
True
>>>
>>> isinstance(alice, Admin)
False
>>>
>>> isinstance(alice, User|Admin)
True

17.3.7. Instances of Builtin Classes

>>> a = int()
>>> b = float()
>>> c = bool()
>>> d = str()
>>> e = list()
>>> f = tuple()
>>> g = set()
>>> h = dict()

17.3.8. Use Case - 1

>>> x = list()
>>> y = list()

The above works because someone created class list():

>>> class List:
...     pass
>>>
>>>
>>> x = List()
>>> y = List()

17.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()

17.3.10. Use Case - 4

>>> class Astronaut:
...     pass
>>>
>>>
>>> mark = Astronaut()
>>> melissa = Astronaut()
>>> rick = Astronaut()
>>> alex = Astronaut()
>>> beth = Astronaut()
>>> chris = Astronaut()

17.3.11. Use Case - 5

>>> class AstronautPilot:
...     pass
...
>>> class AstronautScientist:
...     pass
...
>>> class AstronautEngineer:
...     pass
...
>>> class AstronautPhysician:
...     pass
>>>
>>>
>>> mark = AstronautScientist()
>>> melissa = AstronautEngineer()
>>> rick = AstronautPilot()
>>> alex = AstronautScientist()
>>> beth = AstronautEngineer()
>>> chris = AstronautPhysician()

17.3.12. References

17.3.13. Assignments

# %% About
# - Name: OOP Instance One
# - Difficulty: easy
# - Lines: 1
# - 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 instance `alice` of a class `User`
# 1. Run doctests - all must succeed

# %% Polish
# 1. Stwórz instancję `alice` klasy `User`
# 1. Uruchom doctesty - wszystkie muszą się powieść

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

>>> from inspect import isclass

>>> assert isclass(User)
>>> assert isinstance(alice, User)
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
alice: object

# %% Data
class User:
    pass

# %% Result

# %% About
# - Name: OOP Instance Many
# - 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 instance `alice` of a class `User`
# 2. Create instance `bob` of a class `Admin`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Stwórz instancję `alice` klasy `User`
# 2. Stwórz instancję `bob` klasy `Admin`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> 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(alice, User)
>>> assert isinstance(bob, Admin)
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
alice: object
bob: object

# %% Data
class User:
    pass

class Admin:
    pass

# %% Result

# %% About
# - Name: OOP Instance IsInstance
# - 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. Define `result: type` with a result of checking a type of `alice`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: type` z wynikiem sprawdzania typu `alice`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> 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'>
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
result: type

# %% Data
class User:
    pass


alice = User()

# %% Result
result = ...

# %% About
# - Name: OOP Instance IsInstance
# - 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. Define `result: bool` with a result of checking
#    if `alice` is and instance of a class `User`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: bool` z wynikiem sprawdzania,
#    czy `alice` jest instancją klasy `User`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> 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
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
result: bool

# %% Data
class User:
    pass


alice = User()

# %% Result
result = ...

# %% About
# - Name: OOP Instance IsInstance
# - 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. Define `result: bool` with a result of checking
#    if `alice` is and instance of a class `User` or `Admin`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj `result: bool` z wynikiem sprawdzania,
#    czy `alice` jest instancją klasy `User` lub `Admin`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> 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
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% Imports

# %% Types
result: bool

# %% Data
class User:
    pass

class Admin:
    pass


alice = User()

# %% Result
result = ...