[Python-Dev] Re: PEP 279

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:

  + the nice thing about enumerate is that it has the word 'number' in
  - it is not obvious to me at first blush that 'enumerating' and
'iterating' elicit different 'first-guess semantics'
  - The first meaning I'd guess for enumerate(foo) would be

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'