diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2024-02-20 07:53:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-20 07:53:25 (GMT) |
commit | acda1757bc682922292215906459c2735ee99c04 (patch) | |
tree | 4bf91604ac3b8a217521fbf5a64df99af24345de /Doc | |
parent | 2aaef562364b3dea64d7eee1e7dd9e51cb806f91 (diff) | |
download | cpython-acda1757bc682922292215906459c2735ee99c04.zip cpython-acda1757bc682922292215906459c2735ee99c04.tar.gz cpython-acda1757bc682922292215906459c2735ee99c04.tar.bz2 |
gh-113157: Document and test __get__ for MethodType (gh-115492)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/howto/descriptor.rst | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst index 7d787c1..e72386a 100644 --- a/Doc/howto/descriptor.rst +++ b/Doc/howto/descriptor.rst @@ -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 @@ -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: |