Skip to content

Commit 47becca

Browse files
committed
dataclasses exercise
1 parent 3eeccb9 commit 47becca

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from dataclasses import dataclass
2+
from datetime import date
3+
4+
5+
@dataclass(frozen=True)
6+
class Person:
7+
name: str
8+
date_of_birth: date
9+
preferred_operating_system: str
10+
11+
def is_adult(self) -> bool:
12+
today = date.today()
13+
has_had_birthday_this_year = (today.month, today.day) >= (
14+
self.date_of_birth.month,
15+
self.date_of_birth.day,
16+
)
17+
age = today.year - self.date_of_birth.year
18+
if not has_had_birthday_this_year:
19+
age -= 1
20+
return age >= 18
21+
22+
23+
imran = Person("Imran", date(2002, 6, 15), "Ubuntu")
24+
imran2 = Person("Imran", date(2002, 6, 15), "Ubuntu")
25+
eliza = Person("Eliza", date(1990, 11, 20), "Arch Linux")
26+
27+
print(imran)
28+
print(imran == imran2)
29+
print(imran.is_adult())
30+
print(eliza.is_adult())

0 commit comments

Comments
 (0)