from dataclasses import dataclass
from typing import TypedDict
class StudentDict(TypedDict):
Name: str
Old: int
@dataclass
class Student:
name: str
old: int
def encode(self) -> StudentDict:
return StudentDict(Name=self.name, Old=self.old)
@classmethod
def decode(cls, student: StudentDict) -> "Student":
return cls(name=student["Name"], old=student["Old"])
def main():
student = Student(name="John", old=20)
print(student.encode())
print(Student.decode(student.encode()))
if __name__ == "__main__":
main()
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (1)
Hey, this article appears to have been generated with the assistance of ChatGPT or possibly some other AI tool.
We allow our community members to use AI assistance when writing articles as long as they abide by our guidelines. Please review the guidelines and edit your post to add a disclaimer.
Failure to follow these guidelines could result in DEV admin lowering the score of your post, making it less visible to the rest of the community. Or, if upon review we find this post to be particularly harmful, we may decide to unpublish it completely.
We hope you understand and take care to follow our guidelines going forward!