[Python-ideas] dictionary constructor should not allow duplicate keys
luigi at semenzato.com
Tue May 3 21:58:43 EDT 2016
On Tue, May 3, 2016 at 6:40 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> Since the dict created by that dict display happens at run time, I am
> suggesting that during the process of creating that dict that any keys,
> however generated or retrieved, that are duplicates of keys already in the
> dict, cause an appropriate error (to be determined).
> Also, any dict display that is able to be used for dict creation at compile
> time (thereby avoiding the run-time logic) should have the same checks and
> raise the same error if duplicate keys are found (I imagine both run-time
> and compile-time dict creation from dict displays would use the same
> underlying function).
I agree with Ethan that this is the correct way of looking at this
problem, rather than my original, confusing presentation. Duplicate
keys in a dict display should be an error, period. In some cases the
error can be detected at parse time, which is nicer. In other cases
it can be detected only at run time, but it should always be an error.
Also, Steven D'Aprano <steve at pearwood.info> wrote:
> Duplicate keys are special because, unlike the other errors, the dict
> constructor CAN guard against them.
> That would be a reasonable answer. But I'm not sure if it is special
> *enough* to justify violating the Zen of Python. That may be a matter of
> opinion and taste.
Thank you, I will gladly take the "reasonable answer" qualification
offered here. And I agree that often these issues build down to
aesthetics. But it's hard for me to see how the current semantics
support the Zen of Python. I wouldn't presume that all original
design decisions were infallible, and I still haven't heard a single
good argument in favor of *allowing* duplicate keys.
More information about the Python-ideas