summaryrefslogtreecommitdiffstats
path: root/Doc/howto/descriptor.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2020-10-24 01:37:27 (GMT)
committerGitHub <noreply@github.com>2020-10-24 01:37:27 (GMT)
commit8e5b0fdce337ef0a1f4f38b31a8c6b66c56b16d2 (patch)
treec5f68a7ee6731f841b7566ce0ce9317eed82a14a /Doc/howto/descriptor.rst
parent04523c5bd60569468cd8971dfaeec99e51c353fa (diff)
downloadcpython-8e5b0fdce337ef0a1f4f38b31a8c6b66c56b16d2.zip
cpython-8e5b0fdce337ef0a1f4f38b31a8c6b66c56b16d2.tar.gz
cpython-8e5b0fdce337ef0a1f4f38b31a8c6b66c56b16d2.tar.bz2
bpo-19072: Update descriptor howto for decorator chaining (GH-22934)
Diffstat (limited to 'Doc/howto/descriptor.rst')
-rw-r--r--Doc/howto/descriptor.rst16
1 files changed, 13 insertions, 3 deletions
diff --git a/Doc/howto/descriptor.rst b/Doc/howto/descriptor.rst
index 4a53b9e..4e9fad3 100644
--- a/Doc/howto/descriptor.rst
+++ b/Doc/howto/descriptor.rst
@@ -872,6 +872,16 @@ Using the non-data descriptor protocol, a pure Python version of
def __get__(self, obj, cls=None):
if cls is None:
cls = type(obj)
- def newfunc(*args):
- return self.f(cls, *args)
- return newfunc
+ if hasattr(obj, '__get__'):
+ return self.f.__get__(cls)
+ return types.MethodType(self.f, cls)
+
+The code path for ``hasattr(obj, '__get__')`` was added in Python 3.9 and
+makes it possible for :func:`classmethod` to support chained decorators.
+For example, a classmethod and property could be chained together::
+
+ class G:
+ @classmethod
+ @property
+ def __doc__(cls):
+ return f'A doc for {cls.__name__!r}'