Skip to content

Instantly share code, notes, and snippets.

@norbertparti
Created November 25, 2020 13:12
Show Gist options
  • Save norbertparti/96207a17af61e9baefa98786e88c495c to your computer and use it in GitHub Desktop.
Save norbertparti/96207a17af61e9baefa98786e88c495c to your computer and use it in GitHub Desktop.
Convert dict to Python dataclass
@dataclass
class Person:
name: str # mandatory
def __init__(self, **kwargs):
names = set([f.name for f in fields(self)])
for k, v in kwargs.items():
setattr(self, k, v) # dynamically create class fields by passed dict fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment