Skip to content

Commit 416bc20

Browse files
committed
dataclasses
1 parent dedfc11 commit 416bc20

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

dataclasses_ex.py

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

0 commit comments

Comments
 (0)