[Python-Dev] Re: PEP 279 (original) (raw)

David Ascher DavidA@ActiveState.com
Mon, 01 Apr 2002 21:24:54 -0800


Guido van Rossum wrote:

So now I'd like to choose between enumerate() and indexer(). Any closing arguments?

A few thoughts:

One reason I suggested 'indexer' is that the tuples (index, element) are what I think of as an index entry -- it's a pair of (key, the element being located)

A problem with indexer that I just realized is that indexer(dictionary) does return (number, value) pairs, while one might reasonably expect it to generate the same thing as dictionary.iteritems().

>>> print d
{'y': 'foo', 'x': 123, 'z': 1j}
>>> for (k,v) in indexer(d):
...   print k, v
...
0 y
1 x
2 z
>>> for (k,v) in d.iteritems():
...   print k, v
...
y foo
x 123
z 1j
>>> for (k,v) in enumerate(d):
...   print k, v
...
0 y
1 x
2 z

Actually, having the function return the dictionary keys as the second element in the tuple is a little weird. But I guess that's just the consequence of the earlier choice w.r.t. iter(dict).

All things considered, even though I suggested 'indexer()', I'm voting for 'enumerate()' because of the numeric focus as opposed to a 'key' focus.

--da