Skip to content

Commit 378ae21

Browse files
rhettingerLukasWoodtli
authored andcommitted
pythongh-113157: Document and test __get__ for MethodType (pythongh-115492)
1 parent c6c385d commit 378ae21

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

Doc/howto/descriptor.rst

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,10 @@ roughly equivalent to:
11921192
"Emulate method_getattro() in Objects/classobject.c"
11931193
return getattr(self.__func__, name)
11941194

1195+
def __get__(self, obj, objtype=None):
1196+
"Emulate method_descr_get() in Objects/classobject.c"
1197+
return self
1198+
11951199
To support automatic creation of methods, functions include the
11961200
:meth:`__get__` method for binding methods during attribute access. This
11971201
means that functions are non-data descriptors that return bound methods
@@ -1214,8 +1218,20 @@ descriptor works in practice:
12141218
.. testcode::
12151219

12161220
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
12191235

12201236
The function has a :term:`qualified name` attribute to support introspection:
12211237

0 commit comments

Comments
 (0)