Last active
July 29, 2021 05:51
-
-
Save FerdinaKusumah/15a54a9cf90c689e13b48ee07e48910f to your computer and use it in GitHub Desktop.
Python data class
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 | |
@dataclass() | |
class Hobby: | |
name: str = None | |
@dataclass() | |
class Student: | |
name: str = None | |
age: int = None | |
hobby: [Hobby] = None | |
if __name__ == "__main__": | |
h = Hobby() | |
h.name = "swimming" | |
s = Student() | |
s.name = "John toer" | |
s.age = 18 | |
s.hobby = [h.__dict__] | |
result = s.__dict__ | |
# result is | |
# {'name': 'John toer', 'age': 18, 'hobby': [{'name': 'swimming'}]} | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment