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

M.-A. Lemburg mal at egenix.com
Wed May 4 08:37:56 EDT 2016


On 04.05.2016 14:15, Steven D'Aprano wrote:
> On Wed, May 04, 2016 at 01:56:56PM +0200, M.-A. Lemburg wrote:
> 
>> Issuing a warning for this would probably help, but raising
>> an error introduced a backwards incompatibility which is
>> not warranted IMO, given how seldom such situations occur in
>> practice.
>>
>> For the implementation, there are two possibilities I can think
>> of:
>>
>> 1. have STORE_MAP run a test for an already existing entry
>>    and issue a warning
>>
>> 2. add a final CHECK_MAP byte code to check that the number
>>    of keys in the mapping correspond to the number of keys
>>    added via the dict literal
>>
>> The first one causes a lot of overhead, esp. for larger mappings
>> such as the ones used by the codecs or other static data tables.
>> Most of the times these are generated anyway, so the warning
>> case would not occur at all, but you'd still have to check
>> for a collision N times.
>>
>> The second one is cheap regarding performance, but may not
>> be accurate, since STORE_MAP may well be working on dynamically
>> generated keys. It does work well for constants, and if we're
>> just issuing a warning, may be good enough.
> 
> I'm not sure I understand what you mean by this. Are you suggesting that 
> if I write:
> 
> {1: None, 1: None}
> 
> Python knows that it got two keys as argument, and so 
> CHECK_MAP can determine that there is a duplicate? (i.e. that 2 keys 
> were arguments, but the finished dict only has length 1). But if I 
> write:
> 
> {x: None, x: None}
> 
> Python *cannot* tell that there were two keys given (since they are 
> expressions, not constants) and CHECK_MAP can't do anything?

Well, the CHECK_MAP byte code would only check whether the number
of assignments you are doing is equal to the number of keys
found in the final dict. That's a very coarse test and not
perfect. It can also not tell you which key causes the
mismatch.

On the plus side, the extra check is a simply size check
and doesn't have to be applied per added mapping.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, May 04 2016)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> Python Database Interfaces ...           http://products.egenix.com/
>>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
________________________________________________________________________

::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/
                      http://www.malemburg.com/



More information about the Python-ideas mailing list