10.1. Classes and Instances

10.1.1. About

  • Object Oriented Paradigm

  • Model world as objects that interacts with each other

class

Templates for objects.

instance
object

Object created from class.

10.1.2. Classes

  • Capitalized CamelCase name convention

  • Classes are templates for objects

Listing 214. Defining class. Classes should have capitalized name
class Astronaut:
    pass

class Iris:
    pass
Listing 215. Multi-word class names should use CamelCase
class MyClass:
    pass

class IrisSetosa:
    pass

10.1.3. Instances

  • Instances are also known as Objects

  • Two newlines between class and code

  • snake_case names

10.1.3.1. Example with Astronauts

Listing 216. One class and one instance
class Astronaut:
    pass


watney = Astronaut()
Listing 217. One class and three instances
class Astronaut:
    pass


watney =  = Astronaut()
twardowski = Astronaut()
jimenez = Astronaut()
Listing 218. Two classes and two instances
class Astronaut:
    pass

class Cosmonaut:
    pass


mark = Astronaut()
ivan = Cosmonaut()
Listing 219. Two classes and four instances (two instances of an Astronaut class, and two of a Cosmonaut class)
class Astronaut:
    pass

class Cosmonaut:
    pass


mark = Astronaut()
melissa = Astronaut()
ivan = Cosmonaut()
jan = Cosmonaut()

10.1.3.2. Examples with Iris

Listing 220. One class and one instance
class Iris:
    pass


flower = Iris()
Listing 221. One class and three instances
class Iris:
    pass


setosa = Iris()
versicolor = Iris()
virginica = Iris()
Listing 222. Three classes and four instances (two instances of a one class)
class IrisSetosa:
    pass

class IrisVersicolor:
    pass

class IrisVirginica:
    pass


iris_setosa1 = IrisSetosa()
iris_setosa2 = IrisSetosa()
iris_versicolor = IrisVersicolor()
iris_virginica = IrisVirginica()

10.1.4. Class vs Instance

../../_images/blueprint.png

Figure 18. Intuition definition: Class is a blueprint, instances are homes made from this plan. Image source: [ImgFre19]

10.1.5. Good engineering practices

  • 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

Listing 223. Classes and Objects
class IrisSetosa:
    pass

class IrisVersicolor:
    pass

class IrisVirginica:
    pass


setosa = IrisSetosa()
versicolor = IrisVersicolor()
virginica = IrisVirginica()

10.1.6. Assignments

10.1.6.1. Example

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 5 min

  • Solution: solution/class_example.py

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

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

Solution
class Iris:
    pass


setosa = Iris()
versicolor = Iris()
virginica = Iris()
The whys and wherefores

10.1.6.2. Class instantiation

English
  1. Define class Astronaut

  2. Define class SpaceAgency

  3. Create instance twardowski of a class Astronaut

  4. Create instance watney of a class Astronaut

  5. Create instance nasa of a class SpaceAgency

  6. Create instance esa of a class SpaceAgency

  7. Create instance polsa of a class SpaceAgency

Polish
  1. Zdefiniuj klasę Astronaut

  2. Zdefiniuj klasę SpaceAgency

  3. Stwórz instancję twardowski klasy Astronaut

  4. Stwórz instancję watney klasy Astronaut

  5. Stwórz instancję nasa klasy SpaceAgency

  6. Stwórz instancję esa klasy SpaceAgency

  7. Stwórz instancję polsa klasy SpaceAgency

The whys and wherefores