10.2. OOP Attributes

  • Attributes are also known as "Properties" or "Fields"

  • snake_case name convention

  • Attributes should be defined only in __init__() method

  • Attributes store information (state) for instances

  • Access field values using dot (.) notation

10.2.1. Setting attributes

10.2.1.1. Runtime attributes

Listing 98. Dynamic attributes
class Iris:
    pass


flower = Iris()

flower.sepal_length = 5.1
flower.sepal_width = 3.5
flower.petal_length = 1.4
flower.petal_width = 0.2
flower.species = 'setosa'

print(flower.sepal_length)  # 5.1
print(flower.sepal_width)   # 3.5
print(flower.species)       # 'setosa'

10.2.1.2. Init time attributes

Listing 99. 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'


flower = Iris()

print(flower.sepal_length)  # 5.1
print(flower.sepal_width)   # 3.5
print(flower.species)       # 'setosa'

10.2.1.3. Variable value attributes

Listing 100. 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 101. Method argument with default value
class Iris:
    def __init__(self, species=None):
        self.species = species


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

10.2.2. Access modifiers

  • All fields are always public

  • No protected i private

  • _name - protected field (by convention)

  • __name__ - system field

  • name_ - used while name collision

Listing 102. Access modifiers
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'


flower = Iris()

print(flower._sepal_length)  # 5.1       # IDE should warn, that you access protected member
print(flower._sepal_width)   # 3.5       # IDE should warn, that you access protected member
print(flower._petal_length)  # 1.4       # IDE should warn, that you access protected member
print(flower._petal_width)   # 0.2       # IDE should warn, that you access protected member
print(flower.species)       # 'setosa'

10.2.3. __dict__ - Getting dynamic fields and values

Listing 103. __dict__ - Getting dynamic fields and values
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


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

flower.__dict__
# {'sepal_length': 5.1,
# 'sepal_width': 3.5,
# 'petal_length': 1.4,
# 'petal_width': 0.2,
# 'species': 'setosa'}

10.2.4. Assignment

10.2.4.1. Data Modeling

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?

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ć?

Input
Jan, Twardowski, 1961-04-12
Mark, Watney, 1969-07-21
Kennedy Space Center, Merritt Island, FL
Johnson Space Center, Houston, TX
Jet Propulsion Laboratory, Pasadena, CA
The whys and wherefores