Repository navigation
Expand file tree
/
Copy pathadvanced_oops.py
More file actions
321 lines (230 loc) · 7.64 KB
/
Copy pathadvanced_oops.py
File metadata and controls
321 lines (230 loc) · 7.64 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
'''
1. Dunder Methods (Magic Methods)
You already know __init__ and __repr__. Here are the ones interviewers care about:
`
class Money:
def __init__(self, amount, currency="AED"):
self.amount = amount
self.currency = currency
def __repr__(self):
return f"Money({self.amount}, {self.currency})"
def __eq__(self, other):
return self.amount == other.amount and self.currency == other.currency
def __lt__(self, other):
return self.amount < other.amount
def __add__(self, other):
if self.currency != other.currency:
raise ValueError("Cannot add different currencies")
return Money(self.amount + other.amount, self.currency)
def __len__(self):
return self.amount
a = Money(100)
b = Money(50)
print(a + b) # Money(150, AED)
print(a == b) # False
print(a > b) # True
print(len(a)) # 100
`
Key dunder methods to know:
__init__ → constructor
__repr__ → string representation
__str__ → user-friendly string
__eq__ → ==
__lt__ →
__gt__ → >
__add__ → +
__len__ → len()
__hash__ → makes object usable in sets/dict keys
__call__ → makes object callable like a function
2. __call__ — making objects behave like functions:
`
class Multiplier:
def __init__(self, factor):
self.factor = factor
def __call__(self, value):
return value * self.factor
double = Multiplier(2)
print(double(5)) # 10 — calling object like a function!
print(double(100)) # 200
`
Exercise: Create a class LLMCostCalculator that:
Takes price_per_token in constructor
Has __call__ so you can use it like a function — pass token count, returns cost
Has __add__ to combine two calculators (adds their prices)
Has __repr__
'''
class LLMCostCalculator:
def __init__(self, temp, model):
self.temp = temp
self.model = model
def __call__(self, value):
return self.temp * value
def __repr__(self):
return f"LLMCostCalculator({self.temp}, {self.model})"
def __add__(self, other):
return LLMCostCalculator(self.temp+other.temp, 'combined')
def main1():
gpt4 = LLMCostCalculator(0.03, "gpt-4")
mini = LLMCostCalculator(0.01, "gpt-4o-mini")
print(gpt4(1000)) # 30.0 (cost for 1000 tokens)
print(mini(1000)) # 10.0
combined = gpt4 + mini
print(combined) # LLMCostCalculator(0.04, combined)
print(combined(1000)) # 40.0
'''1. @property — Controlled Access to Attributes
Instead of writing get_name() / set_name() like Java, Python uses @property:'''
class User:
def __init__(self, first, last):
self.first = first
self.last = last
@property
def full_name(self):
return f"{self.first} {self.last}"
@full_name.setter
def full_name(self, value):
first, last = value.split(" ")
self.first = first
self.last = last
@full_name.getter
def full_name(self, value):
return f"{self.first} {self.last}"
def main2():
u = User("Deepak", "Raju")
print(u.full_name) # "Deepak Raju" — no parentheses!
u.full_name = "John Doe" # calls the setter
print(u.full_name) # "John"
'''
Why use it?
Looks like an attribute but runs logic behind the scenes
Can add validation without breaking existing code
Read-only properties — just skip the setter
'''
class APIConfig:
def __init__(self, key):
self._key = key # _ means "private by convention"
@property
def key(self):
return self._key
# No setter → read-only!
config = APIConfig("abc123")
# print(config.key) # "abc123"
# config.key = "new" # AttributeError! Read-only
'''
2. dataclasses — Less Boilerplate
Writing __init__, __repr__, __eq__ every time is tedious. dataclass generates them for you:
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4] — both point to same list
Same problem happens with class defaults:
The bug — mutable default shared:
@dataclass
class APIRequest:
endpoint: str
headers: dict = {} # ❌ DANGER
r1 = APIRequest("/products")
r2 = APIRequest("/users")
r1.headers["Authorization"] = "Bearer abc"
print(r2.headers) # {"Authorization": "Bearer abc"}
r1 and r2 share the same {} object in memory. Changing one changes both.
Why does this happen?
# Python creates the default {} ONCE when class is defined
# Not once per instance
headers: dict = {}
# ↑ this ONE dict object is shared by ALL instances
# It's like:
shared_dict = {}
r1.headers = shared_dict # same object
r2.headers = shared_dict # same object
The fix — default_factory creates a NEW dict each time:
@dataclass
class APIRequest:
endpoint: str
headers: dict = field(default_factory=dict) # ✅ SAFE
r1 = APIRequest("/products")
r2 = APIRequest("/users")
r1.headers["Authorization"] = "Bearer abc"
print(r2.headers) # {} — separate object, unaffected ✅
default_factory=dict tells Python: "call dict() fresh for every new instance."
Same applies to lists and sets:
# ❌ Wrong
tags: list = []
items: set = set()
# ✅ Correct
tags: list = field(default_factory=list)
items: set = field(default_factory=set)
Only immutable defaults are safe directly:
# These are fine — immutable, can't be changed
name: str = "default"
count: int = 0
rate: float = 0.5
active: bool = True
coords: tuple = (0, 0)
Quick rule: If the default is dict, list, or set → always use field(default_factory=...).
'''
# from dataclasses import dataclass, field
# @dataclass(frozen=True)
# class Sample:
# abc: str
# xyz: int
# obj: dict = field(default_factory=dict)
# def main():
# s1 = Sample("s",1, {"a":1})
# s2 = Sample("q",2, {"a":2})
# print(s1)
# s1.obj['a']=2
# print(s1)
# print(s2)
'''
__slots__ — Memory Optimization
Normally, Python stores object attributes in a __dict__ (a dictionary). __slots__ replaces it with a fixed array:
# Normal — each object gets its own dict
class User:
def __init__(self, name, age):
self.name = name
self.age = age
u = User("Deepak", 28)
print(u.__dict__) # {"name": "Deepak", "age": 28}
# With __slots__ — no dict, fixed structure
class User:
__slots__ = ["name", "age"]
def __init__(self, name, age):
self.name = name
self.age = age
u = User("Deepak", 28)
u.__dict__ # AttributeError! No dict
u.email = "test" # AttributeError! Can't add new attributes
Normal __slots__
Memory/object ~300 bytes ~100 bytes
Add new attrs Yes No
Speed Slower Faster
When to use: When you create millions of objects (like processing millions of API logs or LLM tokens).
With dataclass:
@dataclass(slots=True) # Python 3.10+
class Token:
text: str
score: float
'''
'''
Exercise: Create a dataclass called LLMResponse that:
Has fields: model (str), content (str), tokens_used (int), cost (float)
Has a default model of "gpt-4o"
Is frozen (immutable)
Has a @property called cost_per_token that returns cost / tokens_used
'''
from dataclasses import dataclass
@dataclass(frozen=True)
class LLMResponse:
content:str
tokens_used:int
cost:float
model:str = "gpt-4o"
@property
def cost_per_token(self):
return self.cost / self.tokens_used
def main():
resp = LLMResponse(content="Hello world", tokens_used=50, cost=1.5)
print(resp) # shows all fields
print(resp.model) # "gpt-4o"
print(resp.cost_per_token) # 0.03
resp.content = "new" # should raise error (frozen)