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

Steven D'Aprano steve at pearwood.info
Wed May 4 09:50:12 EDT 2016


On Tue, May 03, 2016 at 06:58:43PM -0700, Luigi Semenzato wrote:

> I still haven't heard a single
> good argument in favor of *allowing* duplicate keys.

Because duplicate keys are not necessarily an error. Maybe you don't 
care if there are duplicates. Maybe you expect duplicates, and want 
the last one seen to win.

Sometimes experienced programmers forget what it is like to be a 
beginning programmer. One of the most useful things a beginner can do is 
experiment at the interactive interpreter. The more things the language 
prohibits, the more errors the beginner gets, the less they learn, and 
the more intimidating the language is. Duplicate keys are not 
necessarily an error. It just means one value or the other is dropped.

How do dict displays construct the dict? Do they add keys from left to 
right, or right to left? I can read the source, if I can find it, and 
locate the right section of the right file, and if I can read C. Or I 
can do:

py> {1: "left", 1: "right"}
{1: 'right'}

and in one line of code in the Python interactive interpreter I've 
learned more than in probably an hour of digging through C code.

To a beginner, being able to do things like this without Big Brother 
shouting at them "No! That's Wrong! Have An Exception!!!" all the time 
is literally without price. Who cares that it's a useless thing to do in 
production code? "Useless" is not an error.

Errors, and warnings, should be left for things which really are errors.


Even if you think my example doesn't matter, and you think that 
duplicate keys are always an error, it may be that this case is not bad 
enough to prohibit. You don't get this check for free. It requires code, 
and that code has to run, and it will run for every single dict display 
whether needed or not. Depending on how it is implemented, it might be 
quite expensive.

All those programmers who have carefully inspected their source code to 
ensure that the data used in the dict is correct, with no duplicates and 
no misspellings and no missed keys, they have no need of this check. 
But you would force it on them anyway, because one of your colleagues 
was sloppy and made a mistake and had difficulty debugging it. Okay, I 
have sympathy for you and your colleague, but that doesn't mean its okay 
for everyone to carry the cost of checking for your error.

Maybe it is justified. Maybe it isn't.

If you are really serious that you think duplicate keys are an error, 
then to be consistent you should also want to raise an error in the dict 
constructor, dict(obj, **kwargs). If not, what is your reasoning for 
why duplicate keys should be allowed in dict(...)?


-- 
Steve


More information about the Python-ideas mailing list