16.2. OOP Class 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.

16.2.1. Class vs Instance

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

Figure 16.1. Class. Source: [1]

../../_images/oop-classes-instances.jpg

Figure 16.2. Instances. Source: [2]

16.2.2. One Class, One Instance

One class and one instance:

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

16.2.3. One Class, Many Instances

One class and three instances:

>>> class User:
...     pass
>>>
>>>
>>> mark = User()
>>> melissa = User()
>>> rick = User()

16.2.4. 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.2.5. Naming Convention

>>> first_name = str()
>>> last_name = str()
>>>
>>> firstname = str()
>>> lastname = str()
>>>
>>> fname = str()
>>> lname = str()
>>> class User:
...     pass
...
>>>
>>> mark_watney = User()
>>> melissa_lewis = User()
>>>
>>> markwatney = User()
>>> melissalewis = User()
>>>
>>> mwatney = User()
>>> mlewis = User()

16.2.6. Type vs Isinstance

>>> class User:
...     pass
>>>
>>> mark = User()
>>> isinstance(mark, User)
True
>>> type(mark)
<class '__main__.User'>
>>> type(mark) is User
True
>>>
>>> mark is User
False

16.2.7. Use Case - 0x01

>>> x = int()
>>> y = int()
>>>
>>> x
0
>>> y
0
>>> class Int:
...     pass
...
>>> x = Int()
>>> y = Int()

16.2.8. Use Case - 0x02

>>> x = int()
>>> y = float()
>>>
>>> x
0
>>> y
0.0
>>> class Int:
...     pass
...
>>> class Float:
...     pass
...
>>> x = int()
>>> y = float()

16.2.9. Use Case - 0x03

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

16.2.10. Use Case - 0x04

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

16.2.11. Use Case - 0x05

>>> class AstronautPilot:
...     pass
...
>>> class AstronautScientist:
...     pass
...
>>> class AstronautEngineer:
...     pass
...
>>> class AstronautMedic:
...     pass
>>>
>>>
>>>
>>> mark_watney = AstronautScientist()
>>> melissa_lewis = AstronautEngineer()
>>> rick_martinez = AstronautPilot()
>>> alex_vogel = AstronautScientist()
>>> beth_johanssen = AstronautEngineer()
>>> chris_beck = AstronautMedic()

16.2.12. References

16.2.13. Assignments

"""
* Assignment: OOP Instance One
* Type: class assignment
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Create instance `mark` of a class `Account`
    1. Run doctests - all must succeed

Polish:
    1. Stwórz instancję `mark` klasy `Account`
    1. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

    >>> assert isclass(Account)
    >>> assert isinstance(mark, Account)
"""

class Account:
    pass


# Create instance `mark` of a class `Account`
# type: Account
...


"""
* Assignment: OOP Instance Many
* Type: class assignment
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Create instance `mark` of a class `User`
    2. Create instance `melissa` of a class `Admin`
    3. Create instance `rick` of a class `User`
    4. Run doctests - all must succeed

Polish:
    1. Stwórz instancję `mark` klasy `User`
    2. Stwórz instancję `melissa` klasy `Admin`
    3. Stwórz instancję `rick` klasy `User`
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from inspect import isclass

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

    >>> assert isinstance(mark, User)
    >>> assert isinstance(melissa, Admin)
    >>> assert isinstance(rick, User)
"""

class User:
    pass

class Admin:
    pass


# Create instance `mark` of a class `User`
# type: User
...

# Create instance `melissa` of a class `Admin`
# type: Admin
...

# Create instance `rick` of a class `User`
# type: User
...