-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataclasses_try.py
46 lines (35 loc) · 975 Bytes
/
dataclasses_try.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from dataclasses import dataclass
class OldStyleStudentInfo:
roll_no = 0
name = ''
subjects = []
def __init__(self, roll_no, name, subjects):
self.roll_no = roll_no
self.name = name
self.subjects = subjects
@dataclass(order=True, unsafe_hash=True)
class StudentInfo:
roll_no: int
name: str
subjects: list
std1 = StudentInfo(14,'Vijay',['Maths','Science'])
std2 = StudentInfo(15,'Ajay',['Hindi','English'])
if std1 != std2:
print('2 students are not equal')
#data classes are mutable by default
std1.roll_no=15
std1.name='Ajay'
std1.subjects = ['Hindi', 'English']
if std1 == std2:
print('2 students are equal')
#use frozen parameter to make an immutable data class
@dataclass(frozen=True)
class Marks:
maths: int
english: int
science: int
hindi: int
m_vijay = Marks(30,40,50,60)
#throws dataclasses.FrozenInstanceError: cannot assign to field 'english'
#m_vijay.english=55
print(m_vijay)