Skip to content

Commit 6079044

Browse files
committed
Format generics-exercise1.py with Black and refactor age property
- Add @Property decorator to age() method for attribute-like access - Format long conditional statement across multiple lines - Add blank lines between logical sections for readability - Change child.age() method call to child.age property access - Improve code formatting consistency with Black formatter - Maintain all functionality while improving code style
1 parent ffef3cd commit 6079044

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

prep/generics-exercise1.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@
22
from typing import List
33
from datetime import date
44

5+
56
@dataclass(frozen=True)
67
class Person:
78
name: str
89
date_of_birth: date
910
children: List["Person"]
1011

12+
@property
1113
def age(self) -> int:
1214
today = date.today()
1315
age = today.year - self.date_of_birth.year
14-
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
16+
if (today.month, today.day) < (
17+
self.date_of_birth.month,
18+
self.date_of_birth.day,
19+
):
1520
age -= 1
1621
return age
1722

23+
1824
fatma = Person(name="Fatma", date_of_birth=date(2015, 7, 22), children=[])
1925
aisha = Person(name="Aisha", date_of_birth=date(2018, 3, 10), children=[])
2026

2127
imran = Person(name="Imran", date_of_birth=date(1985, 5, 15), children=[fatma, aisha])
2228

29+
2330
def print_family_tree(person: Person) -> None:
2431
print(person.name)
2532
for child in person.children:
26-
print(f"- {child.name} ({child.age()})")
27-
28-
print_family_tree(imran)
33+
print(f"- {child.name} ({child.age})")
2934

3035

36+
print_family_tree(imran)

0 commit comments

Comments
 (0)