Skip to content

Commit 6f00188

Browse files
committed
incorporating dataclasses in the code
1 parent 77ca2f6 commit 6f00188

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

Sprint5/data-class.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from datetime import date
2+
from dataclasses import dataclass
3+
4+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
birth_day: date
8+
preferred_operating_system: str
9+
10+
def age(self)-> int: #returns age in years
11+
today = date.today()
12+
years = today.year - self.birth_day.year
13+
if(today.month, today.day) < (self.birth_day.month, self.birth_day.day):
14+
years -= 1
15+
return years
16+
17+
def is_adult(self):
18+
return self.age() >= 18
19+
20+
21+
p1 = Person("Sara", date(1996, 10, 28), "macOS")
22+
print(p1.is_adult()) # True
23+
24+
p2 = Person("Muhib", date(2018, 6, 22), "Linux")
25+
print(p2.is_adult()) # False

0 commit comments

Comments
 (0)