[Python-ideas] loosening the restriction on what types may be unpacked using the ** syntax

Eric Snow ericsnowcurrently at gmail.com
Thu Apr 3 08:54:35 CEST 2014


The ** keyword arg unpacking syntax in function calls is useful.
However, currently there is an explicit check for a mapping type
(presumably PyMapping_Check).  So you can pass an object to dict(),
but that same object *may* not work for keyword arg unpacking.  For
example:

>>> from collections import OrderedDict
>>> def f(**kwargs): return kwargs
...
>>> kwargs = ((chr(i), i) for i in range(65, 68))
>>> f(**kwargs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() argument after ** must be a mapping, not generator
>>> f(**OrderedDict(kwargs))
{'B': 66, 'A': 65, 'C': 67}

I'd like to relax this restriction so that anything you can pass to
dict is also valid for keyword arg unpacking.  This would mean you
don't have to first create a dict to wrap the object before passing it
in.

Thoughts?

-eric


More information about the Python-ideas mailing list