Skip to content

Commit f422356

Browse files
committed
Add dataclasses exercise
1 parent 0ab20fb commit f422356

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)