-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathmyDataclasses.py
More file actions
28 lines (19 loc) · 707 Bytes
/
myDataclasses.py
File metadata and controls
28 lines (19 loc) · 707 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from datetime import date
from dataclasses import dataclass
thisYear = date.today().year
@dataclass
class Person:
name: str
DoB: int
preferred_operating_system: str
def __init__(self, name: str, age: int, preferred_operating_system: str):
self.name = name
self.DoB = thisYear - age
self.preferred_operating_system = preferred_operating_system
def is_adult(self):
return self.DoB <= thisYear - 18
imran = Person("Imran", 22, "Ubuntu") # We can call this constructor - @dataclass generated it for us.
print(imran)
imran2 = Person("Imran", 22, "Ubuntu")
print(imran2)
print(imran == imran2) # Prints True because they have the same DoB