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 d4d96c8 commit 2518525Copy full SHA for 2518525
1 file changed
generics.py
@@ -0,0 +1,26 @@
1
+
2
+#EXERCISE 1: Fix the above code so that it works. You must not change the print on line 17
3
+# we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children)
4
5
+# SOLUTION:
6
7
+from dataclasses import dataclass
8
+from typing import List
9
10
+@dataclass(frozen=True)
11
+class Person:
12
+ name: str
13
+ age: int = 0
14
+ children: List["Person"]
15
16
+fatma = Person(name="Fatma", age=18, children=[])
17
+aisha = Person(name="Aisha", age=24, children=[])
18
19
+imran = Person(name="Imran", age=45, children=[fatma aisha])
20
21
+def print_family_tree(person: Person) -> None:
22
+ print(person.name)
23
+ for child in person.children:
24
+ print(f"- {child.name} ({child.age})")
25
26
+print_family_tree(imran)
0 commit comments