Skip to content

Commit 93e5244

Browse files
committed
generics exercise
1 parent 47becca commit 93e5244

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from dataclasses import dataclass
2+
from datetime import date
3+
from typing import List
4+
5+
6+
@dataclass(frozen=True)
7+
class Person:
8+
name: str
9+
date_of_birth: date
10+
children: List["Person"]
11+
12+
@property
13+
def age(self) -> int:
14+
today = date.today()
15+
has_had_birthday_this_year = (today.month, today.day) >= (
16+
self.date_of_birth.month,
17+
self.date_of_birth.day,
18+
)
19+
years = today.year - self.date_of_birth.year
20+
if not has_had_birthday_this_year:
21+
years -= 1
22+
return years
23+
24+
25+
fatma = Person(name="Fatma", date_of_birth=date(2017, 5, 3), children=[])
26+
aisha = Person(name="Aisha", date_of_birth=date(2019, 9, 12), children=[])
27+
28+
imran = Person(name="Imran", date_of_birth=date(1991, 2, 10), children=[fatma, aisha])
29+
30+
def print_family_tree(person: Person) -> None:
31+
print(person.name)
32+
for child in person.children:
33+
print(f"- {child.name} ({child.age})")
34+
35+
print_family_tree(imran)

0 commit comments

Comments
 (0)