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

Steven D'Aprano steve at pearwood.info
Wed May 4 08:15:25 EDT 2016


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?



-- 
Steve


More information about the Python-ideas mailing list