9.1. OOP Classes and Instances

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

>>> class MyClass:
...     pass
>>>
>>>
>>> my = MyClass()

9.1.2. Classes

  • Capitalized CamelCase name convention

  • Classes are templates for objects

Defining class. Classes should have capitalized name:

>>> class Astronaut:
...     pass

Multi-word class names should use CamelCase:

>>> class AstronautPilot:
...     pass

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

9.1.4. Class vs Instance

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

Figure 9.1. Class. Source: [class]

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

Figure 9.2. Instances. Source: [instances]

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

9.1.6. Use Cases

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

9.1.7. Assignments

Code 9.1. Solution
"""
* Assignment: OOP Class Iris
* Filename: oop_class_iris.py
* 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. Compare result with "Tests" section (see below)

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. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> from inspect import isclass
    >>> assert isclass(Iris)
    >>> assert isinstance(setosa, Iris)
    >>> assert isinstance(versicolor, Iris)
    >>> assert isinstance(virginica, Iris)
"""


Code 9.2. Solution
"""
* Assignment: OOP Class Instance
* Filename: oop_class_instance.py
* 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. Compare result with "Tests" section (see below)

Polish:
    1. Zdefiniuj klasę `Astronaut`
    2. Zdefiniuj klasę `SpaceAgency`
    3. Stwórz instancję `watney` klasy `Astronaut`
    4. Stwórz instancję `nasa` klasy `SpaceAgency`
    5. Porównaj wyniki z sekcją "Tests" (patrz poniżej)

Tests:
    >>> from inspect import isclass
    >>> assert isclass(Astronaut)
    >>> assert isclass(SpaceAgency)
    >>> assert isinstance(watney, Astronaut)
    >>> assert isinstance(nasa, SpaceAgency)
"""