Skip to content

Commit 0a25d71

Browse files
committed
task: Complete step 6 prep exercises
1 parent b642106 commit 0a25d71

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

prep-exercises/generics.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ------------------------
2+
# Fix the above code so that it works. You must not change the print on line 17 - we do want to print the children’s ages. (Feel free to invent the ages of Imran’s children.)
3+
# ------------------------
4+
5+
# A.
6+
7+
from dataclasses import dataclass
8+
from typing import List
9+
10+
11+
@dataclass(frozen=True)
12+
class Person:
13+
name: str
14+
age: int
15+
children: List["Person"]
16+
17+
18+
fatma = Person(name="Fatma", age=11, children=[])
19+
aisha = Person(name="Aisha", age=6, children=[])
20+
21+
imran = Person(name="Imran", age=43, children=[fatma, aisha])
22+
23+
24+
def print_family_tree(person: Person) -> None:
25+
print(person.name)
26+
for child in person.children:
27+
print(f"- {child.name} ({child.age})")
28+
29+
30+
print_family_tree(imran)

0 commit comments

Comments
 (0)