9.1. Classes and Instances

9.1.1. Rationale

  • Object Oriented Paradigm

  • Model world as objects that interacts with each other


Templates for objects.


Object created from class.

class MyClass:

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:
Listing 9.2. Multi-word class names should use CamelCase
class AstronautPilot:

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:

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

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

class Cosmonaut:

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:

class CosmonautPilot:

mark_watney = AstronautPilot()
melissa_lewis = AstronautPilot()
ivan_ivanovich = CosmonautPilot()
jan_twardowski = CosmonautPilot()

9.1.4. Class vs Instance


Figure 9.1. Class. Source: [class]


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:

class AstronautScientist:

class AstronautPilot:

mark_watney = AstronautScientist()
melissa_lewis = AstronautScientist()
jose_jimenez = AstronautEngineer()
jan_twardowski = AstronautPilot()
def hello():

class Astronaut:

mark = Astronaut()
jose = Astronaut()

9.1.6. Assignments OOP Class Iris

  • Assignment name: OOP Class Iris

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 5 lines

  • Estimated time of completion: 3 min

  • Solution: solution/oop_class_iris.py

  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

  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 OOP Class Instance

  • Assignment name: OOP Class Instance

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 9 lines

  • Estimated time of completion: 5 min

  • Solution: solution/oop_class_instance.py

  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

  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