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

Rob Cliffe rob.cliffe at btinternet.com
Wed May 4 05:05:53 EDT 2016

On 04/05/2016 07:35, Ethan Furman wrote:
> On 05/03/2016 09:20 PM, Michael Selik wrote:
>> On Wed, May 4, 2016 at 12:04 AM Nick wrote:
>>> As such, the technical change being requested here (at least for
>>> CPython), is that BUILD_MAP be updated to do a PyDict_Contains()
>>> check prior to each key insertion, and:
>>> 1. Report a DeprecationWarning in 3.6
>>> 2. Report a [RuntimeError? ValueError?] in 3.7+
>> To be clear, this implementation would potentially raise an error in the
>> random example -- ``{random(): 0, random(): 1}`` -- correct? If so, then
>> code smell or not, I think that's too much breakage.
> 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.
Rob Cliffe

More information about the Python-ideas mailing list