Skip to content

Commit 2518525

Browse files
committed
generics
1 parent d4d96c8 commit 2518525

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

generics.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)