Author maggyero
Recipients docs@python, eric.araujo, ezio.melotti, maggyero, mdk, rhettinger, willingc
Date 2019-06-08.11:51:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1559994687.73.0.550711459072.issue37203@roundup.psfhosted.org>
In-reply-to
Content
With the current Python equivalent `ClassMethod` implementation of `classmethod` given in Raymond Hettinger's _Descriptor HowTo Guide_, the following code snippet:

```
class A:
    @ClassMethod
    def f(cls, *, x): pass

print(A.f)
A.f(x=3)
```

prints:

> <function ClassMethod.\_\_get\_\_.<locals>.newfunc at 0x106b76268>

and raises:

> TypeError: newfunc() got an unexpected keyword argument 'x'

instead of only printing:

> <bound method A.f of <class '\_\_main\_\_.A'>>

like the `@classmethod` decorator would do.

So the `ClassMethod` implementation fails in two regards:
* it does not return a bound method to a class;
* it does not handle keyword-only arguments.

With this PR `ClassMethod` will correctly emulate `classmethod`. This approach (`types.MethodType`) is already used in the Python equivalent `Function` implementation of functions given earlier in the same guide.
History
Date User Action Args
2019-06-08 11:51:27maggyerosetrecipients: + maggyero, rhettinger, ezio.melotti, eric.araujo, docs@python, willingc, mdk
2019-06-08 11:51:27maggyerosetmessageid: <1559994687.73.0.550711459072.issue37203@roundup.psfhosted.org>
2019-06-08 11:51:27maggyerolinkissue37203 messages
2019-06-08 11:51:27maggyerocreate