[Python-ideas] dictionary constructor should not allow duplicate keys
Paul Moore
p.f.moore at gmail.com
Wed May 4 05:48:22 EDT 2016
On 4 May 2016 at 10:05, Rob Cliffe <rob.cliffe at btinternet.com> wrote:
>> Either fix it all the way or don't bother. The rule "duplicates are allowed" or
>> "duplicates are not allowed" is much simpler and easier to remember than
>> "duplicates are allowed except when..." or, contrariwise, "duplicates are not
>> allowed unless ...".
>
> I disagree, on the grounds that 'practicality beats purity' (sorry to repeat
> myself).
> Here's a thought.
> We are rightly concerned with changes breaking existing code.
> The proposed change could lead to some existing code being *repaired*, by
> pointing out a mistake that the author doesn't know is there.
The problem is that it could also break code that works at the moment.
Consider a mapping of constants to names:
colors = {
RED: "Red",
BLUE: "Blue",
PINK: "Pink",
LIGHT_BLUE: "Light Blue",
DARK_BLUE: "Dark Blue",
}
On a certain output device, there may be no "light blue", and in that
case the code sets LIGHT_BLUE = BLUE. That's entirely reasonable, and
the author may be perfectly happy with the current behavior in that
case.
So I think it's important for backward compatibility that this change
be *only* for the case of literal values as keys (which is what the OP
proposed, of course). And given that it needs to just be for literal
values as keys, I personally think that makes it a case of "Special
cases aren't special enough to break the rules", so I'm -0 on the
proposal (I don't think it's a *bad* thing, in the constants-only
form, just that it's not worth the effort - but if someone else makes
the effort, I won't object).
Paul
More information about the Python-ideas
mailing list