generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathdataclasses_.py
More file actions
23 lines (18 loc) · 712 Bytes
/
dataclasses_.py
File metadata and controls
23 lines (18 loc) · 712 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from dataclasses import dataclass
from datetime import date
@dataclass(frozen=True)
class Person:
name: str
date_of_birth: date
preferred_operating_system: str
def is_adult(self) -> bool:
today = date.today()
age = today.year - self.date_of_birth.year - (
(today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day)
)
return age >= 18
imran = Person("Imran", date(2002, 5, 15), "Ubuntu")
imran2 = Person("Imran", date(2002, 5, 15), "Ubuntu")
print(imran) # Person(name='Imran', date_of_birth=datetime.date(2002, 5, 15), preferred_operating_system='Ubuntu')
print(imran == imran2) # True
print(imran.is_adult()) # True