Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8e5b0fd

Browse files
authored
bpo-19072: Update descriptor howto for decorator chaining (GH-22934)
1 parent 04523c5 commit 8e5b0fd

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Doc/howto/descriptor.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,16 @@ Using the non-data descriptor protocol, a pure Python version of
872872
def __get__(self, obj, cls=None):
873873
if cls is None:
874874
cls = type(obj)
875-
def newfunc(*args):
876-
return self.f(cls, *args)
877-
return newfunc
875+
if hasattr(obj, '__get__'):
876+
return self.f.__get__(cls)
877+
return types.MethodType(self.f, cls)
878+
879+
The code path for ``hasattr(obj, '__get__')`` was added in Python 3.9 and
880+
makes it possible for :func:`classmethod` to support chained decorators.
881+
For example, a classmethod and property could be chained together::
882+
883+
class G:
884+
@classmethod
885+
@property
886+
def __doc__(cls):
887+
return f'A doc for {cls.__name__!r}'

0 commit comments

Comments
 (0)