This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author barry
Recipients barry, docs@python
Date 2013-07-22.18:51:43
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1374519103.74.0.277776306074.issue18531@psf.upfronthosting.co.za>
In-reply-to
Content
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.

http://docs.python.org/3/reference/compound_stmts.html#function-definitions
http://docs.python.org/3/reference/expressions.html#calls

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.
History
Date User Action Args
2013-07-22 18:51:43barrysetrecipients: + barry, docs@python
2013-07-22 18:51:43barrysetmessageid: <1374519103.74.0.277776306074.issue18531@psf.upfronthosting.co.za>
2013-07-22 18:51:43barrylinkissue18531 messages
2013-07-22 18:51:43barrycreate