3.1. Static and Dynamic Fields

3.1.1. Static Fields

  • Fields created on class

  • Simple to use

  • Must have default values

  • Share state

Listing 460. Static Fields
class Astronaut:
    agency = 'NASA'


watney = Astronaut()
jimenez = Astronaut()

print(watney.agency)
# NASA

print(jimenez.agency)
# NASA

print(Astronaut.agency)
# NASA

3.1.2. Dynamic Fields

  • Fields created on instance

  • Do not share state

  • By convention initialized in __init__()

  • You can also initialize on living object directly

Listing 461. Dynamic fields
class Astronaut:
    def __init__(self, agency):
        self.agency = agency


watney = Astronaut('NASA')
twardowski = Astronaut('POLSA')

print(watney.agency)
# NASA

print(twardowski.agency)
# POLSA

print(Astronaut.agency)
# AttributeError: type object 'Astronaut' has no attribute 'agency'

3.1.3. Static vs. Dynamic Fields

Listing 462. Static vs. Dynamic fields
class Astronaut:
    agency = 'NASA'


watney = Astronaut()
twardowski = Astronaut()
ivanovic = Astronaut()

# Check value of field agency
watney.agency       # NASA
twardowski.agency   # NASA
ivanovic.agency     # NASA
Astronaut.agency    # NASA

# Let's change ``agency`` of ``ivanovich`` object
ivanovic.agency = 'Roscosmos'

watney.agency       # NASA
twardowski.agency   # NASA
ivanovic.agency     # Roscosmos
Astronaut.agency    # NASA

# Let's change ``agency`` of ``Astronaut`` class
Astronaut.agency = 'POLSA'

watney.agency       # POLSA
twardowski.agency   # POLSA
ivanovic.agency     # Roscosmos
Astronaut.agency    # POLSA

3.1.4. Static or Dynamic?

class Astronaut:
    first_name = ...
    last_name = ...
class Cosmonaut:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
class Taikonaut:
    pass

t = Taikonaut()
t.first_name = ...
t.last_name = ...
class Taikonaut:
    pass

Taikonaut.first_name
Taikonaut.last_name
from dataclasses import dataclass


@dataclass
class Astronaut:
    name: str
    missions: list