-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdataclasses_in_python.py
More file actions
130 lines (109 loc) · 2.6 KB
/
Copy pathdataclasses_in_python.py
File metadata and controls
130 lines (109 loc) · 2.6 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# class without dataclass
"""
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Name:{self.name}, Age:{self.age}"
per1 = Person("Arun", 28)
print(per1) #Name:Arun, Age:28
"""
#class with dataclass
"""
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
per1 = Person("Arun", 28)
print(per1) #Person(name='Arun', age=28)
"""
# default values and immutability
"""
from dataclasses import dataclass
@dataclass(frozen=True) #makes immutable
class Product:
name: str
price: float = 0.0 #default value
in_stock: bool = True #default
item = Product("Phone", 999)
print(item) #Product(name='Phone', price=999, in_stock=True)
# This will raise an error because frozen=True makes it immutable
item.price = 1000 #FrozenInstanceError
"""
# class with custom methods
"""
from dataclasses import dataclass
@dataclass
class Employee:
name: str
emp_id: int
salary: float
def give_raise(self, percentage):
self.salary+=self.salary*(percentage/100)
emp = Employee("Arun", 101, 50000)
print(emp) #Employee(name='Arun', emp_id=101, salary=50000)
emp.give_raise(10)
print(emp) #Employee(name='Arun', emp_id=101, salary=55000.0)
"""
# nested dataclasses
"""
from dataclasses import dataclass
@dataclass
class Company:
reg_name: str
city: str
@dataclass
class Employee:
name: str
role: str
company: Company
com = Company("RB", "BNG")
emp = Employee("Arun", "dev", com)
print(emp)
#Employee(name='Arun', role='dev', company=Company(reg_name='RB', city='BNG'))
"""
# ordering and comparison
"""
from dataclasses import dataclass
@dataclass(order=True)
class Employee:
name: str
salary: int
emp1 = Employee("Arunisto", 5000)
emp2 = Employee("Arun", 1000)
print(emp2 > emp1)
#False (Compares attributes in order: name, salary)
print(sorted([emp1, emp2]))
"""
"""
result:
[Employee(name='Arun', salary=1000), Employee(name='Arunisto', salary=5000)]
"""
# post-initialization with __post_init__
"""
from dataclasses import dataclass
@dataclass
class Rectangle:
width: int
height: int
area: int = 0 # will be compute
def __post_init__(self):
self.area = self.width*self.height
rect = Rectangle(40, 50)
print(rect) #Rectangle(width=40, height=50, area=2000)
"""
# inheritance with dataclass
from dataclasses import dataclass
@dataclass
class Vehicle:
make: str
year: int
@dataclass
class Car(Vehicle):
model: str
doors: int = 4
car = Car(make="Toyota", year=2020, model="Camry")
print(car)
#Car(make='Toyota', year=2020, model='Camry', doors=4)