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

Rob Cliffe rob.cliffe at btinternet.com
Tue May 3 04:00:56 EDT 2016



On 03/05/2016 08:22, Michel Desmoulin wrote:
> That would introduce a very weird special case. All those would work:
>
> foo = 1
> bar = "1"
>
> {1: True, 2 - 1: True, foo: True, int(bar): True, int("1"): True, **{1:
> True}}
>
> But not this:
>
> {1: True, 1: True}
Perfectly true.
>
> It would be very confusing.
It could conceivably confuse someone, but the example quoted by the OP 
with all literal keys is realistic and yours is contrived.
There is practical benefit in spotting duplicate literal keys, if it can 
be done with either an error or a warning (whether at compile time or 
run time).
There is even a kind of analogy:

     dict(lion=1, lion=2)
SyntaxError: keyword argument repeated

Is that also confusing?



More information about the Python-ideas mailing list