12.2. OOP Class Instance

  • Instances are also known as Objects

  • Two newlines between class and code

  • snake_case names

instance
object

Object created from class.

12.2.1. Class vs Instance

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

Figure 12.1. Class. Source: 1

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

Figure 12.2. Instances. Source: 2

12.2.2. Instances

One class and one instance:

>>> class Astronaut:
...     pass
>>>
>>>
>>> watney = Astronaut()

One class and three instances:

>>> class Astronaut:
...     pass
>>>
>>>
>>> watney = Astronaut()
>>> lewis = Astronaut()
>>> martinez = Astronaut()

Two classes and two instances:

>>> class Astronaut:
...     pass
>>>
>>> class Cosmonaut:
...     pass
>>>
>>>
>>> watney = Astronaut()
>>> twardowski = Cosmonaut()

Two classes and four instances (two instances of an Astronaut class, and two of a Cosmonaut class):

>>> class SeniorAstronaut:
...     pass
>>>
>>> class SeniorCosmonaut:
...     pass
>>>
>>>
>>> mark_watney = SeniorAstronaut()
>>> melissa_lewis = SeniorAstronaut()
>>> pan_twardowski = SeniorCosmonaut()
>>> class AstronautEngineer:
...     pass
>>>
>>> class AstronautScientist:
...     pass
>>>
>>> class AstronautPilot:
...     pass
>>>
>>>
>>> mark_watney = AstronautScientist()
>>> melissa_lewis = AstronautScientist()
>>> rick_martinez = AstronautPilot()
>>> alex_vogel = AstronautEngineer()

12.2.3. Use Case - 0x01

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

12.2.4. References

1

http://makieta.pl/12344-thickbox_default/faller-130803-blok-z-wielkiej-plyty-skala-h0.jpg

2

https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Os_Rusa_Poznań_RB1.jpg/1200px-Os_Rusa_Poznań_RB1.jpg

12.2.5. Assignments

Code 12.3. Solution
"""
* Assignment: OOP Class Iris
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 2 min

English:
    1. Create instance `setosa` of a class `Iris`
    2. Create instance `virginica` of a class `Iris`
    3. Create instance `versicolor` of a class `Iris`
    4. Run doctests - all must succeed

Polish:
    1. Stwórz instancję `setosa` klasy `Iris`
    2. Stwórz instancję `virginica` klasy `Iris`
    3. Stwórz instancję `versicolor` klasy `Iris`
    4. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Iris)
    >>> assert isinstance(setosa, Iris)
    >>> assert isinstance(versicolor, Iris)
    >>> assert isinstance(virginica, Iris)
"""

class Iris:
    pass



Code 12.4. Solution
"""
* Assignment: OOP Class Instance
* Required: yes
* Complexity: easy
* Lines of code: 2 lines
* Time: 2 min

English:
    1. Define class `Astronaut`
    2. Define class `SpaceAgency`
    3. Create instance `watney` of a class `Astronaut`
    4. Create instance `nasa` of a class `SpaceAgency`
    5. Run doctests - all must succeed

Polish:
    1. Zdefiniuj klasę `Astronaut`
    2. Zdefiniuj klasę `SpaceAgency`
    3. Stwórz instancję `watney` klasy `Astronaut`
    4. Stwórz instancję `nasa` klasy `SpaceAgency`
    5. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert isclass(Astronaut)
    >>> assert isclass(SpaceAgency)
    >>> assert isinstance(watney, Astronaut)
    >>> assert isinstance(nasa, SpaceAgency)
"""

class Astronaut:
    pass


class SpaceAgency:
    pass