Created
November 25, 2020 13:12
-
-
Save norbertparti/96207a17af61e9baefa98786e88c495c to your computer and use it in GitHub Desktop.
Convert dict to Python dataclass
This file contains 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
@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