Skip to content

Commit 761797a

Browse files
committed
Compelted "Dataclasses" exercise.
1 parent fbb3825 commit 761797a

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from dataclasses import dataclass
2+
from datetime import date
3+
4+
@dataclass(frozen=True)
5+
class Person:
6+
name: str
7+
date_of_birth: date
8+
preferred_operating_system: str
9+
10+
def is_adult(self):
11+
today = date.today()
12+
age = today.year - self.date_of_birth.year
13+
14+
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
15+
age -= 1
16+
17+
return age >= 18
18+
19+
imran = Person("Imran", date(2004, 6, 28), "Ubuntu") # We can call this constructor - @dataclass generated it for us.
20+
print(imran) # Prints Person(name='Imran', age=22, preferred_operating_system='Ubuntu')
21+
22+
imran2 = Person("Imran", date(2004, 6, 28), "Ubuntu")
23+
print(imran == imran2) # Prints True

0 commit comments

Comments
 (0)