Skip to content

Commit d4d96c8

Browse files
committed
data_classes
1 parent 5e2a021 commit d4d96c8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

data-classes.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Write a Person class using @datatype which uses a datetime.date for date of birth, rather than an int for age.
3+
# Re-add the is_adult method to it.
4+
5+
from datetime import date
6+
from dataclasses import dataclass
7+
8+
@dataclass (frozen=True)
9+
class Person:
10+
name: str
11+
date_of_birth: date
12+
preferred_operating_system: str
13+
14+
def is_adult(self) -> bool:
15+
today = date.today()
16+
age = today.year - self.date_of_birth.year
17+
18+
# Adjust if birthday hasn't happened yet this year
19+
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
20+
age -= 1
21+
return age >= 18
22+
23+
jesus = Person("Jesus", date(1980, 1, 12), "Ubuntu")
24+
print(jesus.is_adult())
25+

0 commit comments

Comments
 (0)