Skip to content

Commit 9096891

Browse files
committed
feat: Used dataclass decorator instead of the manual _init_.
1 parent 82dae40 commit 9096891

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

prep-exercises/dataclass.py

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

0 commit comments

Comments
 (0)