[docs] [issue18531] Undocumented different between METH_KEYWORDS and **kws

Barry A. Warsaw report at bugs.python.org
Mon Jul 22 20:51:43 CEST 2013

New submission from Barry A. Warsaw:

A colleague discovered an interesting implementation different between C-defined functions and Python-defined functions called with **kws arguments.  He tried to do this:

>>> from collections import defaultdict
>>> '{foo}{bar}'.format(**defaultdict(str))

He wondered how this could work, especially given:

>>> def what(**kws):
...   print(type(kws), repr(kws))
>>> what(**defaultdict(str))
<class 'dict'> {}

The Language Reference clearly states that when what() is called, a new dictionary is create, which is populated by keyword arguments not consumed by positional args.


What isn't described is the behavior when the function is defined in C using METH_KEYWORDS.  In that case, the object passed through the ** argument is passed unscathed to the underlying methodobject defined to take METH_KEYWORDS.  str.format() is of the latter type so it gets the actual defaultdict.  what() of course gets the defined behavior.

It would be nice if the implementation details of the function did not leak into this behavior, but this is admittedly a corner case of the CPython implementation.  I haven't checked any of the alternative implementations to see what they do.  My guess is that CPython won't change for practical and backward compatibility reasons, thus I've opened this issue as a documentation bug.  At the very least, an implementation note in the Language Reference should be added.

assignee: docs at python
components: Documentation
messages: 193559
nosy: barry, docs at python
priority: normal
severity: normal
status: open
title: Undocumented different between METH_KEYWORDS and **kws
versions: Python 2.7, Python 3.3, Python 3.4

Python tracker <report at bugs.python.org>

More information about the docs mailing list