9.3. Attributes

9.3.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 Init Method

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.

namespace

Container for storing related data

Listing 9.12. 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.3.2. Syntax

class Astronaut:
    pass


astro = Astronaut()

astro.firstname = 'Mark'
astro.lastname = 'Watney'

print(astro.firstname)
print(astro.lastname)

9.3.3. Dynamic Attributes

Listing 9.13. Dynamic attributes
class Astronaut:
    pass


jose = Astronaut()
jose.firstname = 'José'
jose.lastname = 'Jiménez'

print(f'My name... {jose.firstname} {jose.lastname}')
# My name... José Jiménez
Listing 9.14. Accessing not existing attributes
class Astronaut:
    pass


astro = Astronaut()

print(astro.missions)
# Traceback (most recent call last):
#     ...
# 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}')
# Traceback (most recent call last):
#     ...
# AttributeError: 'Astronaut' object has no attribute 'name'

9.3.4. Namespace

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)

9.3.5. Different Types

Listing 9.15. Dynamic attributes
class Iris:
    pass


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

print(setosa.label)
# setosa

print(setosa.features)
# [5.1, 3.5, 1.4, 0.2]

sum(setosa.features)
# 10.2
class Astronaut:
    pass


jose = Astronaut()
jose.age = 36

mark = Astronaut()
mark.age = 42.1

9.3.6. Get All Dynamic Attributes and Values

  • obj.__dict__

Listing 9.16. __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.3.7. Assignments

9.3.7.1. OOP Attribute Model

  • Assignment name: OOP Attribute Model

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 15 lines

  • Estimated time of completion: 8 min

  • Solution: solution/oop_attribute_model.py

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. Ile jest klas?

  5. Ile jest instancji?

  6. Wypisz wszystkie pola każdej z instancji

Input
Mark Watney, USA, 1969-07-21
National Aeronautics and Space Administration, USA, 1958-07-29

Jan Twardowski, Poland, 1961-04-12
Polish Space Agency, Poland, 2014-09-26
The whys and wherefores