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

Pavol Lisy pavol.lisy at gmail.com
Tue May 3 16:28:11 EDT 2016


2016-05-03 21:21 GMT+02:00, Michael Selik <michael.selik at gmail.com>:
[...]
> Rob Cliffe mentioned the fact that repeated keyword arguments causes a
> syntax error. I see the parallel here, except that keyword arguments must
> be valid identifiers. Literal dicts syntactically may have any expression
> as the key. Creating a special case for the parser to enforce uniqueness of
> number and string literals as keys seems more trouble than its worth.

Just for curiosity: keys could be valid identifier, could be different
(at least in one POV) and still cause Syntax error as keyword argument
repeated.

{'ij':1, 'ij':2} # this is dict with 2 keys and 2 different values

dict(ij=1, ij=2) # SyntaxError: keyword argument repeated

It is because parser is doing NFKC normalization for non-ascii
identifiers - see https://www.python.org/dev/peps/pep-3131/

('\u0069\u006a' == 'ij' != 'ij' == '\u0133')


More information about the Python-ideas mailing list