We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 25a4c27 commit ee8ecceCopy full SHA for ee8ecce
chapter10-using-types-for-safety-and-inspection/dataclasses2.py
@@ -0,0 +1,17 @@
1
+import inspect
2
+
3
4
+def build_dataclass_init[T](cls: type[T]) -> str:
5
+ annotations = inspect.get_annotations(cls)
6
7
+ args: list[str] = ["self"]
8
+ body: list[str] = []
9
10
+ for name, type in annotations.items():
11
+ args.append(f"{name}: {type.__name__}")
12
+ body.append(f" self.{name} = {name}")
13
14
+ return "def __init__({}) -> None:\n{}".format(
15
+ ", ".join(args),
16
+ "\n".join(body),
17
+ )
0 commit comments