[Python-ideas] Syntax for key-value iteration over mappings
steve at pearwood.info
Tue Jul 28 06:05:10 CEST 2015
On Tue, Jul 28, 2015 at 05:44:13AM +1000, Chris Angelico wrote:
> On Tue, Jul 28, 2015 at 5:17 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> > On Tue, Jul 28, 2015 at 02:25:00AM +1000, Chris Angelico wrote:
> >> What my suggestion was positing was not so much DWIM as "iterate over
> >> the keys and values of anything". A mapping type has a concept of keys
> >> and values; an indexable sequence (list, tuple, etc) uses sequential
> >> numbers as indices and its members as values.
> > .............^^^^^^^
> > Keys and indices are not the same thing, and Python is not Lua.
> > ...
> > Despite the apparent analogy of key <=> index, it's remarkable hard to
> > think of any practical use for such a thing. I cannot think of any time
> > I have wanted to, or might want to in the future, ducktype lists as
> > dicts, with the indices treated as keys.
> A namedtuple is completely different from a list, too. But you can
> iterate over both. [...]
Yes? What's your point? I fail to see how any of this is relevant to the
analogy "indices of a sequence are mapping keys".
Can you give a non-contrived, non-toy, practical example of where
someone might want to seemlessly interchange (key,value) pairs from a
mapping and (index,item) pairs from a sequence and expect to do
something useful? Toy programming exercises like "print a table of
key/index and value/item" aside:
That's a nice exercise for beginners, but doesn't justify new syntax.
As I point out with the example of Lua, you can get quite far with the
analogy "consecutive integer mapping keys are like indices". It's the
other way which is dubious: indices aren't like keys in general, and I
don't think there are many, if any, use-cases for treating sequences
(let alone sets, let alone arbitrary iterables) as if they were a
special case of mapping.
But, you're proposing this. It shouldn't be up to me to prove that it's
not useful. It should be up to you to prove that it is.
> Many types have a concept of keys/indices and their associated values.
> Yes, you're right, removing an element from a list changes the indices
> of all those after it; but the same goes for any sort of mutation of
> an iterable. With a dict, adding a new key/value pair can change the
> order of all the others.
The order of a mapping is generally not part of it's API. Your
observation that adding an item to a dict may change the order of other
items is not relevant. The point I am making is that deleting a key from
a mapping doesn't change the *keys* of all the other items:
does not change the key 1 into 0, or key 2 into 1. But
does change the index of the items. Indices don't behave like keys! If
you want to unify (key,value) and (index,item) as special cases of the
same kind of thing, then you need to
- justify how they can be the same when they behave so differently; and
- explain how this makes Python a better language.
More information about the Python-ideas