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

David Mertz mertz at gnosis.cx
Wed May 4 10:36:04 EDT 2016


After following the long thread, I'm solidly -1, at least on an error.  A
warning wouldn't be so bad, but would still feel like a nuisance not a
feature.  Such a warning should belong to linters, not to the language
itself.  Use those to automatically catch statically detectible
probably-bad patterns like duplicate literals as keys of dictionary
displays.

The thing that nudged me to -1 is code something like the following, which
I've actually written for completely valid use-cases:

def code_generate(data_source, output=open('data.py','w')):
    """Write data to loadable Python module

    data_source may contain multiple occurrences of each key.

    Later occurrences are "better" than earlier ones

    """
    print("data = {", file=output)
    for key, val in data_source:
        print("    %s: %s," % (key, val), file=output)
    print("}", file=output)


I really don't want the Python interpreter to complain about my perfectly
valid 'data.py' module that I auto-generated knowing full well it might
have duplicate literal keys.  Yes, of course I could write a more complex
function that explicitly pruned keys if they were duplicates before writing
to 'data.py'.  But I do not want to have to think about that when a well
defined semantics of Python since version 0.9 does not require that extra
work.

On Wed, May 4, 2016 at 4:56 AM, M.-A. Lemburg <mal at egenix.com> 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.
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160504/38db0374/attachment.html>


More information about the Python-ideas mailing list