File tree 2 files changed +23
-7
lines changed
2 files changed +23
-7
lines changed Original file line number Diff line number Diff line change 1
1
.. _descriptorhowto :
2
2
3
- ======================
4
- Descriptor HowTo Guide
5
- ======================
3
+ ================
4
+ Descriptor Guide
5
+ ================
6
6
7
7
:Author: Raymond Hettinger
8
8
:Contact: <python at rcn dot com>
@@ -1192,6 +1192,10 @@ roughly equivalent to:
1192
1192
"Emulate method_getattro() in Objects/classobject.c"
1193
1193
return getattr(self.__func__, name)
1194
1194
1195
+ def __get__(self, obj, objtype=None):
1196
+ "Emulate method_descr_get() in Objects/classobject.c"
1197
+ return self
1198
+
1195
1199
To support automatic creation of methods, functions include the
1196
1200
:meth: `__get__ ` method for binding methods during attribute access. This
1197
1201
means that functions are non-data descriptors that return bound methods
@@ -1214,8 +1218,20 @@ descriptor works in practice:
1214
1218
.. testcode ::
1215
1219
1216
1220
class D:
1217
- def f(self, x):
1218
- return x
1221
+ def f(self):
1222
+ return self
1223
+
1224
+ class D2:
1225
+ pass
1226
+
1227
+ .. doctest ::
1228
+ :hide:
1229
+
1230
+ >>> d = D()
1231
+ >>> d2 = D2()
1232
+ >>> d2.f = d.f.__get__ (d2, D2)
1233
+ >>> d2.f() is d
1234
+ True
1219
1235
1220
1236
The function has a :term: `qualified name ` attribute to support introspection:
1221
1237
Original file line number Diff line number Diff line change 1
1
.. _sortinghowto :
2
2
3
- Sorting HOW TO
4
- **************
3
+ Sorting Techniques
4
+ ******************
5
5
6
6
:Author: Andrew Dalke and Raymond Hettinger
7
7
You can’t perform that action at this time.
0 commit comments