Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Last active July 29, 2021 05:51
Show Gist options
  • Save FerdinaKusumah/15a54a9cf90c689e13b48ee07e48910f to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/15a54a9cf90c689e13b48ee07e48910f to your computer and use it in GitHub Desktop.
Python data class
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