[Python-ideas] Syntax for key-value iteration over mappings

Stephen J. Turnbull stephen at xemacs.org
Tue Jul 28 11:46:17 CEST 2015


Nick Coghlan writes:

 > Looking up the original iterator PEP shows it was done to enforce the
 > container invariant "for x in y: assert x in y".

I would argue that in the context of a given mapping, <key> and
[<key>, <value>] are equivalent when <value> is <map>[<key>], so that
we shou ld have the signature __contains__(self, key, value=self[key]),
which is a NameError, but the intent should be obvious.

The evident suggestion that we distinguish between tuples (which can
be used as keys) and items by implementing the latter as lists (which
can't, at least in the case of dictionaries) seems fragile and icky,
though, and unacceptable at this point since it's incompatible with
the .items() iterator.


More information about the Python-ideas mailing list