[docs] [issue37203] Correct classmethod emulation in Descriptor HowTo Guide

Géry report at bugs.python.org
Sat Jun 8 07:51:27 EDT 2019


New submission from Géry <gery.ogam at gmail.com>:

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.

----------
assignee: docs at python
components: Documentation
messages: 345031
nosy: docs at python, eric.araujo, ezio.melotti, maggyero, mdk, rhettinger, willingc
priority: normal
pull_requests: 13785
severity: normal
status: open
title: Correct classmethod emulation in Descriptor HowTo Guide
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37203>
_______________________________________


More information about the docs mailing list