Skip to content

Commit bb6b842

Browse files
committed
generics.py
1 parent 1e56b0d commit bb6b842

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

sprint5-prep-exercises/generics.py

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 typing import List
3+
from datetime import date
4+
5+
6+
@dataclass(frozen=True)
7+
class Person:
8+
name: str
9+
date_of_birth: date
10+
children: List["Person"]
11+
12+
def getAge(self) -> int:
13+
current_date = date.today()
14+
full_years = current_date.year - self.date_of_birth.year
15+
if (current_date.month < self.date_of_birth.month) or (
16+
current_date.month == self.date_of_birth.month
17+
and current_date.day < self.date_of_birth.day
18+
):
19+
full_years -= 1
20+
return full_years
21+
22+
23+
fatma = Person(name="Fatma", date_of_birth=date(2020, 4, 12), children=[])
24+
aisha = Person(name="Aisha", date_of_birth=date(2024, 7, 5), children=[])
25+
26+
imran = Person(name="Imran", date_of_birth=date(2000, 1, 30), children=[fatma, aisha])
27+
28+
29+
def print_family_tree(person: Person) -> None:
30+
print(person.name)
31+
for child in person.children:
32+
print(f"- {child.name} ({child.getAge()})")
33+
34+
35+
print_family_tree(imran)

0 commit comments

Comments
 (0)