[Python-ideas] dictionary constructor should not allow duplicate keys

Ethan Furman ethan at stoneleaf.us
Wed May 4 08:25:06 EDT 2016


On 05/03/2016 11:39 PM, Jonathan Goble wrote:
> On Wed, May 4, 2016 at 2:32 AM, Ethan Furman wrote:
>> On 05/03/2016 09:18 PM, Random832 wrote:

>>> To play devil's advocate for a minute here...
>>>
>>> Why *don't* we allow duplicate keyword arguments in a function call,
>>> anyway? CALL_FUNCTION doesn't seem to actually care, if I create this
>>> situation by monkey-patching the calling function's consts to get
>>> duplicate keywords. All the arguments that have been raised here for
>>> *allowing* duplicate keyword arguments seem to apply just as well to
>>
>>>
>>>
>>> f(**{'a': 1}, **{'a': 2})
>>
>>
>> Actually, I believe that is allowed now, although I'm not sure how far it
>> goes.
>
> As of Python 3.5.1, it is still not allowed:
>
> C:\Users\Jonathan>python
> Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900
> 64 bit (AMD64)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> def func(**kwargs):
> ...     pass
> ...
>>>> func(**{'a': 1}, **{'a': 2})
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> TypeError: func() got multiple values for keyword argument 'a'

Ah, right.  The PEP was changed:

   http://bugs.python.org/issue2292#msg234413

--
~Ethan~



More information about the Python-ideas mailing list