Author Luigi Semenzato
Recipients Luigi Semenzato
Date 2016-05-02.17:50:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1462211417.21.0.537077061483.issue26910@psf.upfronthosting.co.za>
In-reply-to
Content
This was already discussed and rejected at https://bugs.python.org/issue16385.  I am reopening because I am not convinced that the discussion presented all arguments properly.

The original problem description:

lives_in = { 'lion': ['Africa', 'America],
             'parrot': ['Europe'],
             #... 100+ more rows here
             'lion': ['Europe'],
             #... 100+ more rows here
           }

The above constructor overwrites the 'lion' entry silently, often causing unexpected behavior.

These are the arguments presented in favor of the rejection, followed by my rebuttals.

1. "An error is out of the question for compatibility reasons".  No real rebuttal here, except I wonder if exceptions are ever made and under what circumstances.  I should point out that a warning may also create incompatibilities.

2. "There are ways to rewrite this as a loop on a list".  Yes of course, but the entire point of the dictionary literal is to offer a concise and convenient notation for entering a dictionary as program text.

3. "Being able to re-write keys is fundamental to Python dicts and why they can be used for Python's mutable namespaces".  This is fine but it applies to the data structure, not the literal constructor.

4. "A code generator could depend on being able to write duplicate keys without having to go back and erase previous output".  Yes, but it seems wrong to facilitate automated code generation at the expense of human code writing.  For hand-written code, I claim that in most (all?) cases it would be preferable to have the compiler detect key duplications.  It is easier for an automated code generator to check for duplications than it is for a human.

5. "There is already pylint".  True, but it forces an additional step, and seems like a cop-out for not wanting to do the "right thing" in the language.

For context, someone ran into this problem in my team at Google.  We fixed it using pylint, but I really don't see the point of having these constructor semantics.  From the discussions I have seen, it seems just an oversight in the implementation/specification of dictionary literals (search keywords: dict constructor, dict literal).  I'd be happy to hear stronger reasoning in favor of the status quo.
History
Date User Action Args
2016-05-02 17:50:17Luigi Semenzatosetrecipients: + Luigi Semenzato
2016-05-02 17:50:17Luigi Semenzatosetmessageid: <1462211417.21.0.537077061483.issue26910@psf.upfronthosting.co.za>
2016-05-02 17:50:17Luigi Semenzatolinkissue26910 messages
2016-05-02 17:50:15Luigi Semenzatocreate