forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecial_methods.py
More file actions
executable file
·420 lines (305 loc) · 8.37 KB
/
special_methods.py
File metadata and controls
executable file
·420 lines (305 loc) · 8.37 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#!/usr/bin/env python
"""
The methods here are of form __XXX__ and are often accessed via operators such as `<`,
or are used by global functions such as `str()` (should be inheritance like Java...).
Full list:
http://docs.python.org/2/reference/datamodel.html#special-method-names
"""
class A(object):
def __init__(self, a=0, b=1):
self.a = a
self.b = b
#def __cmp__(self, other):
#"""
#deprecated in 3., forget it!
#"""
def __eq__(self, other):
"""
>>> a = A()
>>> b = A()
>>> a == b
"""
return self.a == other.a
def __ge__(self, other):
"""
defines >=
"""
return
def __gt__(self, other):
"""
defines >
"""
return
def __le__(self, other):
"""
defines <=
"""
return
def __lt__(self, other):
"""
defines <
"""
return
def __ne__(self, other):
"""
defines !=
"""
return
def __add__(self, other):
"""
>>> A(1,2) + A(3,4)
"""
def __hash__(self, other):
"""
makes hashable, allowing it to be for example a dictionnary key
"""
return
def __str__(self):
"""should return bytes in some encoding,
called string for compatibility, changed to __bytes__ in python 3"""
return unicode(self).encode('utf-8')
def __unicode__(self):
"""informal description, return (possibly unicode) chars
http://stackoverflow.com/questions/1307014/python-str-versus-unicode
changed to __str__ in python 3
"""
return 'a'
def __repr__(self):
"""
Difference from str: formal and very precise string represenation of the object.
What you get if you put an object on a interctive session directly:
>>> A()
class A()
"""
return self.a + ' ' + self.b
def __len__(self):
"""
>>> len(a)
"""
return len(self.a)
d = {}
def __setitem__(self, k, v):
"""
>>> self[k] = v
"""
d[k] = v
def __getitem__(self, k):
"""
>>> self[k]
"""
return self.d[k]
def __contains__(self, v):
"""
>>> v in self
"""
return v in d
def __call__(self, n):
"""
Allows object to be callable as:
>>> a = A()
>>> a(1) == 2
>>> a.__call__(1) == 2
"""
return n + 1
"""
Special attributes which shall not be discussed in this class
because they deserved a more involved discussion:
- `__slots__`
- `__get__`, `__set__` and `__delete__`
"""
if '## equality operator for classes ## __eq__':
"""
Default does not compare member by member,
compares adress of object.
"""
class C:
def __init__(self,i):
self.a = i
c = C(1)
c2 = C(1)
assert c != c2
c = c2
assert c == c2
if '## compare by all members automatically do this':
class C:
def __init__(self, i=0, j=1):
self.i = i
self.j = j
def __eq__(self, other):
"""
all attributes of objects are equal
"""
if type(other) is type(self):
return self.__dict__ == other.__dict__
return False
c = C(1)
c2 = C(1)
assert c == c2
c2 = C(2)
assert c != c2
if '## __eq__ and None':
"""
always compare to `None` with is, never with `==`, because `==` can be overwriden by `__eq__`
for example, to always true, while `is` cannot
http://jaredgrubb.blogspot.com.br/2009/04/python-is-none-vs-none.html
"""
class A(object):
def __eq__(self, other):
return True
a = A()
assert not a is None
assert a == None
if '## __ne__':
# *Not* automatically deduced from __eq__.
class C:
def __eq__(self, other):
return True
assert C() == C()
assert C() != C()
if '## descriptors ## __get__ ## __set__ ## __delete__':
"""
Allow to control what the dot `.` does on access, assignment and del
of an attribute.
Descriptor protocol:
descr.__get__(self, obj, type=None) --> value
descr.__set__(self, obj, value) --> None
descr.__delete__(self, obj) --> None
"""
class Desc(object):
def __init__(self):
self.i = 0
def __get__(self, obj, cls=None):
return self.i + 1
def __set__(self, obj, val):
self.i = val*val
def __delete__(self, obj):
self.i = 0
class HasDesc(object):
i = Desc()
o = HasDesc()
o.i = 2
assert o.i == 5
del o.i
assert o.i == 1
#TODO what do obj and cls do?
"""
Only work for new style classes
"""
class Desc():
def __init__(self):
self.i = 0
def __get__(self, obj, cls=None):
return self.i + 1
class HasDesc():
i = Desc()
o = HasDesc()
assert o.i != 1
if '## property':
"""
Allows to make descriptors with a single class.
"""
assert type(property) == type
class HasDesc(object):
def __init__(self):
self._i = 0
def geti(self):
return self._i + 1
def seti(self, val):
self._i = val * val
def deletei(self):
self._i = 0
i = property(geti, seti, deletei, "doc")
o = HasDesc()
o.i = 2
assert o.i == 5
del o.i
assert o.i == 1
if '## property as decorators':
# It is very idiomatic to use property as a decorator as follows:
class HasDesc(object):
def __init__(self):
self._i = 0
@property
def i(self):
return self._i + 1
@i.setter
def i(self, val):
self._i = val * val
@i.deleter
def i(self):
self._i = 0
o = HasDesc()
o.i = 2
assert o.i == 5
del o.i
assert o.i == 1
# Application: create a readonly value:
class HasDesc(object):
def __init__(self):
self._i = 0
@property
def i(self):
return self._i + 1
o = HasDesc()
assert o.i == 1
try:
o.i = 2
except AttributeError:
pass
else:
assert False
if '## __slots__':
"""
Only available in new style classes.
Fixes exactly what attributes a class can have.
Only to be used as a memory optimization tool when
there are many many objects of a given type in a performance critical point.
"""
class Foo(object):
__slots__ = ['x']
def __init__(self, n):
self.x = n
y = Foo(1)
assert y.x == 1
y.x = 2
assert y.x == 2
# This would work for an object without `__slots__`.
try:
y.z = 3
except AttributeError:
pass
else:
assert False
if '## operators that cannot be overloaded':
"""
<http://stackoverflow.com/questions/3993239/python-class-override-is-behavior>
- is: always implements id compairison.
- not, and and or: only depend on the truth value assignment of objects,
which depends on `__notzero__` and `__len__` in Python 2.
- assignment: makes no sense since assignment always replaces one object for another.
"""
class A(object):
def __is__(self):
return True
def __not__(self):
return True
def __and__(self, other):
return True
def __or__(self, other):
return True
#assert not A()
#assert A() and A()
#assert A() or A()
if '## automaticaly print all members of objects':
class C:
def __init__(self, i=0, j=1):
self.i = i
self.j = j
def __str__(self):
out = '\n' + 30 * '-' + '\n'
for k in self.__dict__.keys():
out += k + ':\n'
out += str(self.__dict__[k]) + '\n\n'
return out
print C()
print C(1,2)