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

João Santos jmcs at jsantos.eu
Wed May 4 08:07:39 EDT 2016

I think "special cases aren't special enough to break the rules" make this
a -1, there's no reason for the behaviour change besides enabling bad code.

On 4 May 2016 at 11:48, Paul Moore <p.f.moore at gmail.com> wrote:

> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160504/92e672bb/attachment.html>

More information about the Python-ideas mailing list