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

Terry Reedy tjreedy at udel.edu
Tue May 3 22:20:42 EDT 2016

On 5/3/2016 8:23 PM, Steven D'Aprano wrote:

>> One just doesn't expect duplicate keys to not raise:
>> --> dict(one=1, one='uno')
>>   File "<stdin>", line 1
>> SyntaxError: keyword argument repeated
> Huh, I had completely forgotten that duplicate keyword arguments raise
> a syntax error. I thought you got a runtime TypeError.

In a sense, it should be.

 >>> def f(x): pass

 >>> f(1, x=2)
Traceback (most recent call last):
   File "<pyshell#12>", line 1, in <module>
     f(1, x=2)
TypeError: f() got multiple values for argument 'x'

But the case of multiple values via duplicate keywords can be and is 
caught when parsing, and parse errors are always (except maybe for ...) 

Terry Jan Reedy

More information about the Python-ideas mailing list