10.3. Initializer Method

  • It's a first method run after object is initiated

  • All classes has default __init__()

  • __init__() is not a constructor!

10.3.1. Initializer method without arguments

Listing 208. Initializer method without arguments
class Iris:
    def __init__(self):
        print('Latin name: Iris Setosa')


flower = Iris()
# Latin name: Iris Setosa

10.3.2. Initializer method with arguments

Listing 209. Initializer method with arguments
class Iris:
    def __init__(self, species):
        print(f'Latin name: {species}')


setosa = Iris('Iris Setosa')
# Latin name: Iris Setosa

virginica = Iris(species='Iris Virginica')
# Latin name: Iris Virginica

iris = Iris()
# TypeError: __init__() missing 1 required positional argument: 'species'
Listing 210. Initializing fields on instance creation
class Iris:
    def __init__(self, species):
        self.species = species


setosa = Iris(species='setosa')
print(setosa.species)
# setosa

virginica = Iris('virginica')
print(virginica.species)
# virginica

versicolor = Iris()
# TypeError: __init__() missing 1 required positional argument: 'species'
Listing 211. Method argument with default value
class Iris:
    def __init__(self, species='versicolor'):
        self.species = species


versicolor = Iris()
print(versicolor.species)
# versicolor

10.3.3. Initializing Attributes

Listing 212. Init time attributes
class Iris:
    def __init__(self):
        self.sepal_length = 5.1
        self.sepal_width = 3.5
        self.petal_length = 1.4
        self.petal_width = 0.2
        self.species = 'setosa'


setosa = Iris()

print(setosa.sepal_length)      # 5.1
print(setosa.sepal_width)       # 3.5
print(setosa.petal_length)      # 1.4
print(setosa.petal_width)       # 0.2
print(setosa.species)           # setosa

print(setosa.xxx)               # AttributeError: 'Iris' object has no attribute 'xxx'
Listing 213. Init time attributes
class Iris:
    def __init__(self):
        self.sepal_length = 5.1
        self.sepal_width = 3.5
        self.petal_length = 1.4
        self.petal_width = 0.2
        self.species = 'setosa'


setosa = Iris()
virginica = Iris()

print(setosa.sepal_length)      # 5.1
print(setosa.sepal_width)       # 3.5
print(setosa.petal_length)      # 1.4
print(setosa.petal_width)       # 0.2
print(setosa.species)           # setosa

print(virginica.sepal_length)   # 5.1
print(virginica.sepal_width)    # 3.5
print(virginica.petal_length)   # 1.4
print(virginica.petal_width)    # 0.2
print(virginica.species)        # setosa
Listing 214. Init time attributes
class Iris:
    def __init__(self, a, b, c, d, e):
        self.sepal_length = a
        self.sepal_width = b
        self.petal_length = c
        self.petal_width = d
        self.species = e


setosa = Iris(5.1, 3.5, 1.4, 0.2, 'setosa')
virginica = Iris(5.8, 2.7, 5.1, 1.9, 'virginica')

print(setosa.sepal_length)      # 5.1
print(setosa.sepal_width)       # 3.5
print(setosa.petal_length)      # 1.4
print(setosa.petal_width)       # 0.2
print(setosa.species)           # setosa

print(virginica.sepal_length)   # 5.8
print(virginica.sepal_width)    # 2.7
print(virginica.petal_length)   # 5.1
print(virginica.petal_width)    # 1.9
print(virginica.species)        # virginica
Listing 215. Init time attributes
class Iris:
    def __init__(self, sepal_length, sepal_width,
                 petal_length, petal_width, species):

        self.sepal_length = sepal_length
        self.sepal_width = sepal_width
        self.petal_length = petal_length
        self.petal_width = petal_width
        self.species = species


setosa = Iris(
    sepal_length=5.1,
    sepal_width=3.5,
    petal_length=1.4,
    petal_width=0.2,
    species='setosa')

virginica = Iris(
    sepal_length=5.8,
    sepal_width=2.7,
    petal_length=5.1,
    petal_width=1.9,
    species='virginica')


print(setosa.sepal_length)      # 5.1
print(setosa.sepal_width)       # 3.5
print(setosa.petal_length)      # 1.4
print(setosa.petal_width)       # 0.2
print(setosa.species)           # setosa

print(virginica.sepal_length)   # 5.8
print(virginica.sepal_width)    # 2.7
print(virginica.petal_length)   # 5.1
print(virginica.petal_width)    # 1.9
print(virginica.species)        # virginica

10.3.4. Checking values

class Kelvin:
    def __init__(self, value):
        if value < 0.0:
            raise ValueError('Kelvin temperature must be greater than 0')
        else:
            self.value = value

ice = Kelvin(273.15)
print(ice)
# 273.15

not_existing = Kelvin(-300)
# ValueError: Kelvin temperature must be greater than 0'

10.3.5. Assignment

10.3.5.1. Classes and instances

English
  1. Create one class Temperature

  2. Create three instances of Temperature class

  3. Values must be passed at the initialization

  4. At initialization instances print:

    1. Instance celsius prints temperature 36.6

    2. Instance fahrenheit prints temperature 97.88

    3. Instance kelvin prints temperature 309.75

  5. Do not convert units

  6. Do not store values in the instances

Polish
  1. Stwórz jedną klasę Temperature

  2. Stwórz trzy instancje klasy Temperature

  3. Wartości mają być podawane przy inicjalizacji

  4. Przy inicjalizacji instancje wypisują:

    1. Instancja celsius wyświetla temperaturę 36.6

    2. Instancja fahrenheit wyświetla temperaturę 97.88

    3. Instancja kelvin wyświetla temperaturę 309.75

  5. Nie konwertuj jednostek

  6. Nie przechowuj informacji w instancjach

10.3.5.2. Data Modeling

  • Complexity level: easy

  • Lines of code to write: 15 lines

  • Estimated time of completion: 10 min

  • Filename: solution/init_model.py

English
  1. Model the data using classes

  2. Create instances for each record

  3. How many classes are there?

  4. How many instances are there?

  5. Create instances of a first class using positional arguments

  6. Create instances of a second class using keyword arguments

  7. Print first field from each instance

Polish
  1. Zamodeluj dane za pomocą klas

  2. Stwórz instancje dla każdego wpisu

  3. Jak wiele klas możemy wyróżnić?

  4. Jak wiele instancji możemy wyróżnić?

  5. Twórz instancje pierwszej klasy używając argumentów pozycyjnych

  6. Twórz instancje drugiej klasy używając argumentów nazwanych

  7. Wypisz pierwsze pole każdej z instancji

Input
Jan, Twardowski, 1961-04-12
Mark, Watney, 1969-07-21
ESA, European Space Agency, Europe
NASA, National Aeronautics and Space Administration, USA
POLSA, Polish Space Agency, Poland
The whys and wherefores