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

Ethan Furman ethan at stoneleaf.us
Wed May 4 02:35:45 EDT 2016

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 ...".


More information about the Python-ideas mailing list