Skip to content

Commit 1f1dd9d

Browse files
committed
DataClasses
1 parent 0d47463 commit 1f1dd9d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

722 Bytes
Binary file not shown.

type/myDataclasses.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
from datetime import date
3+
from dataclasses import dataclass
4+
5+
thisYear = date.today().year
6+
7+
8+
9+
@dataclass
10+
class Person:
11+
name: str
12+
DoB: int
13+
preferred_operating_system: str
14+
15+
def __init__(self, name: str, age: int, preferred_operating_system: str):
16+
self.name = name
17+
self.DoB = thisYear - age
18+
self.preferred_operating_system = preferred_operating_system
19+
20+
def is_adult(self):
21+
return self.DoB <= thisYear - 18
22+
23+
imran = Person("Imran", 22, "Ubuntu") # We can call this constructor - @dataclass generated it for us.
24+
print(imran)
25+
26+
imran2 = Person("Imran", 22, "Ubuntu")
27+
print(imran2)
28+
print(imran == imran2) # Prints True because they have the same DoB

0 commit comments

Comments
 (0)