Skip to content

gh-113157: Document and test __get__ for MethodType #115492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,10 @@ roughly equivalent to:
"Emulate method_getattro() in Objects/classobject.c"
return getattr(self.__func__, name)

def __get__(self, obj, objtype=None):
"Emulate method_descr_get() in Objects/classobject.c"
return self

To support automatic creation of methods, functions include the
:meth:`__get__` method for binding methods during attribute access. This
means that functions are non-data descriptors that return bound methods
Expand All @@ -1214,8 +1218,20 @@ descriptor works in practice:
.. testcode::

class D:
def f(self, x):
return x
def f(self):
return self

class D2:
pass

.. doctest::
:hide:

>>> d = D()
>>> d2 = D2()
>>> d2.f = d.f.__get__(d2, D2)
>>> d2.f() is d
True

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

Expand Down