Thought on PEP 204 and 276

holger krekel pyth at devel.trillke.net
Tue May 28 10:00:12 EDT 2002


Steve Horne wrote:
> On Mon, 27 May 2002 21:33:47 +0200, holger krekel
> <pyth at devel.trillke.net> wrote:
> 
> >       for i,item in enumerate(somelist):
> >            # use item
> >            somelist[i]=newvalue
> 
> Could enumerate(dictionary) be used to extract key, value pairs from a
> dictionary?
> 
> I realise this duplicates the items() method, but it has the advantage
> of creating consistency between sequence and mapping types.

that was precisely my argument on the python-dev list but
it got rejected by GvR personally ... "Trust me. Don't go there."

I realize that mapping types and sequence types are quite different
despite appearances.  It may not be a good idea to blur the lines 
between these two too much. 

> Also, why wasn't an items() method simply added to sequence types,
> treating them as mapping types with an integer key?

that would be ambivalent. calling items on a list could also mean
that you are refering to the elements (no notion of enumeration).

There was a long long discussion about names where 'itemize' and 'enum'
were other hot candidates. In the end i think that we usually have
the problem of going through a list where we sometimes want
to modify it (instead of just walking over them).
And 'enumerate' comes in quite naturally to me, now. It's hardly ever
the case that you don't (want to) know whether some object is a mapping
or a sequence type. 

regards, 

    holger





More information about the Python-list mailing list