13.1. OOP Classes and Instances

13.1.1. Rationale

  • Object Oriented Paradigm

  • Model world as objects that interacts with each other

class

Templates for objects.

instance
object

Object created from class.

13.1.2. Classes

  • Classes are templates for objects

  • PascalCase name convention

Classes should have capitalized name:

>>> class Astronaut:
...     pass

Multi-word class names should use PascalCase:

>>> class AstronautPilot:
...     pass

13.1.3. Instances

  • Instances are also known as Objects

  • Two newlines between class and code

  • snake_case names

One class and one instance:

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

One class and three instances:

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

Two classes and two instances:

>>> class Astronaut:
...     pass
>>>
>>> class Cosmonaut:
...     pass
>>>
>>>
>>> mark = Astronaut()
>>> ivan = Cosmonaut()

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

>>> class AstronautPilot:
...     pass
>>>
>>> class CosmonautPilot:
...     pass
>>>
>>>
>>> mark_watney = AstronautPilot()
>>> melissa_lewis = AstronautPilot()
>>> ivan_ivanovich = CosmonautPilot()
>>> jan_twardowski = CosmonautPilot()

13.1.4. Class vs Instance

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

Figure 13.1. Class. Source: 1

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

Figure 13.2. Instances. Source: 2

13.1.5. Convention

  • Never print in a class

  • One class per file - when classes are long

  • All classes in one file - when classes are short

  • You can mix classes and functions in one file

Classes and Objects:

>>> class AstronautEngineer:
...     pass
>>>
>>> class AstronautScientist:
...     pass
>>>
>>> class AstronautPilot:
...     pass
>>>
>>>
>>> mark_watney = AstronautScientist()
>>> melissa_lewis = AstronautScientist()
>>> jose_jimenez = AstronautEngineer()
>>> jan_twardowski = AstronautPilot()
>>> def hello():
...     pass
>>>
>>> class Astronaut:
...     pass
>>>
>>>
>>> mark = Astronaut()
>>> jose = Astronaut()

13.1.6. Use Cases

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

13.1.7. 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

13.1.8. Assignments

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

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

Polish:
    1. Zdefiniuj klasę `Iris`
    2. Stwórz instancję `setosa` klasy `Iris`
    3. Stwórz instancję `virginica` klasy `Iris`
    4. Stwórz instancję `versicolor` klasy `Iris`
    5. 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)
"""


Code 13.2. Solution
"""
* Assignment: OOP Class Instance
* Required: yes
* Complexity: easy
* Lines of code: 6 lines
* Time: 3 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)
"""