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

Listing 9.1. Defining class. Classes should have capitalized name
class Astronaut:
    pass
Listing 9.2. 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

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


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


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

class Cosmonaut:
    pass


mark = Astronaut()
ivan = Cosmonaut()
Listing 9.6. 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

Listing 9.7. 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. Assignments

9.1.6.1. OOP Class Iris

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

The whys and wherefores

9.1.6.2. OOP Class Instance

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 polsa of a class SpaceAgency

Polish
  1. Zdefiniuj klasę Astronaut

  2. Zdefiniuj klasę SpaceAgency

  3. Stwórz instancję watney klasy Astronaut

  4. Stwórz instancję twardowski klasy Astronaut

  5. Stwórz instancję nasa klasy SpaceAgency

  6. Stwórz instancję polsa klasy SpaceAgency

The whys and wherefores