Skip to content

Commit 1e56b0d

Browse files
committed
dataclasses_ex.py
1 parent 550d3a4 commit 1e56b0d

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

951 Bytes
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
current_date = date.today()
13+
full_years = current_date.year - self.date_of_birth.year
14+
if (current_date.month < self.date_of_birth.month) or (
15+
current_date.month == self.date_of_birth.month
16+
and current_date.day < self.date_of_birth.day
17+
):
18+
full_years -= 1
19+
return full_years >= 18
20+
21+
22+
imran = Person("Imran", date(2008, 1, 30), "Ubuntu")
23+
print(imran)
24+
print("Imran is adult: " + str(imran.is_adult()))

0 commit comments

Comments
 (0)