Skip to content

Instantly share code, notes, and snippets.

@brianfoody
Created October 1, 2020 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianfoody/74e85ca761d4833bd0eee0aa913cf1d8 to your computer and use it in GitHub Desktop.
Save brianfoody/74e85ca761d4833bd0eee0aa913cf1d8 to your computer and use it in GitHub Desktop.
Data classes with replace
from dataclasses import dataclass, replace
from typing import List, Optional
@dataclass(frozen=True)
class State:
val1: float
val2: float
@dataclass
class Controller:
outer1: float
outer2: str
state: Optional[State] = None
controllers: List[Controller] = [
Controller(1, 'H000'),
Controller(2, 'H001'),
Controller(3, 'H002'),
Controller(4, 'H003')
]
states: List[State] = [
State(1, 3),
State(2, 5),
State(3, 7),
State(4, 7)
]
for c in controllers:
for s in states:
c = replace(c, state=s)
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment