Created
July 26, 2021 19:02
-
-
Save infnetdanpro/2b8cfdc5e58d3c2bc0101da0ae484e3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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