9.2. Attributes

9.2.1. Rationale

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

  • snake_case name convention

  • Attributes store information (state) for instances

  • Access field values using dot (.) notation

  • Attributes should be defined only in __init__() method

  • More information in Initializer Method

point_x = 1
point_y = 2
point_z = 3

print(point_x)
print(point_y)
print(point_z)
class Point:
    pass

point = Point()
point.x = 1
point.y = 2
point.z = 3

print(point.x)
print(point.y)
print(point.z)
attribute
field

Variable inside the class. Can be used as a synonym of property or state.

property

Variable inside the class. Should not change during lifetime of an object.

state

Variable inside the class. Changes during lifetime of an object. Represents current state of an object.

Listing 9.8. Class example with distinction of properties and state attributes
Bucket with Water

    Properties:
        - color
        - width
        - height
        - radius
        - capacity
        - net mass (without water)

    State:
        - volume  (how much water is currently in bucket)
        - gross mass = net mass + water mass (water mass depends on its volume used))
../../_images/bucket.jpg

9.2.2. Dynamic Attributes

Listing 9.9. Dynamic attributes
class Temperature:
    pass


temp = Temperature()
temp.kelvin = 10

print(temp.kelvin)
# 10
Listing 9.10. Dynamic attributes
class Astronaut:
    pass


jose = Astronaut()
jose.first_name = 'José'
jose.last_name = 'Jiménez'

print(f'My name... {jose.first_name} {jose.last_name}')
# My name... José Jiménez
Listing 9.11. Dynamic attributes
class Iris:
    pass


setosa = Iris()
setosa.features = [5.1, 3.5, 1.4, 0.2]
setosa.label = 'setosa'

print(setosa.label)
# setosa

sum(setosa.features)
# 10.2
Listing 9.12. Accessing not existing attributes
class Astronaut:
    pass


astro = Astronaut()

print(astro.missions)
# AttributeError: 'Astronaut' object has no attribute 'missions'
class Astronaut:
    pass


jose = Astronaut()
mark = Astronaut()

jose.name = 'José Jiménez'

print(f'My name... {jose.name}')
# My name... José Jiménez

print(f'My name... {mark.name}')
# AttributeError: 'Astronaut' object has no attribute 'name'

9.2.3. Get All Dynamic Attributes and Values

  • obj.__dict__

Listing 9.13. __dict__ - Getting dynamic fields and values
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.__dict__)
# {'sepal_length': 5.1,
#  'sepal_width': 3.5,
#  'petal_length': 1.4,
#  'petal_width': 0.2,
#  'species': 'setosa'}

9.2.4. Assignments

9.2.4.1. Data Modeling

English
  1. Use data from "Input" section (see below)

  2. Model the data using classes

  3. Create instances for each record

  4. How many classes are there?

  5. How many instances are there?

  6. Print all fields from each instance

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Zamodeluj dane za pomocą klas

  3. Stwórz instancje dla każdego wpisu

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

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

  6. Wypisz wszystkie pola 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