Skip to content

Instantly share code, notes, and snippets.

@infnetdanpro
Created July 26, 2021 19:02
Show Gist options
  • Save infnetdanpro/2b8cfdc5e58d3c2bc0101da0ae484e3e to your computer and use it in GitHub Desktop.
Save infnetdanpro/2b8cfdc5e58d3c2bc0101da0ae484e3e to your computer and use it in GitHub Desktop.
from dataclasses import dataclass, field, asdict
@dataclass
class Schedule:
id: int
name: str
@dataclass
class Company:
id: int
name: str
enabled: bool = True
schedule: Schedule = field(default_factory=Schedule)
@dataclass
class User:
id: int
name: str
company: Company = field(default_factory=Company)
if __name__ == '__main__':
schedule = Schedule(id=1, name='Work Schedule')
company = Company(id=1, name='Google', schedule=schedule)
user = User(id=1, name='John', company=company)
print(user.company.schedule.id)
user2 = User(**asdict(user))
print(user2.company.schedule.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment