PEP 276 (was Re: Status of PEP's?)

Raymond Hettinger python at rcn.com
Sat Mar 2 01:46:49 EST 2002


"Carel Fellinger" <cfelling at iae.nl> wrote in message
news:a5orjv$7j0$1 at animus.fel.iae.nl...
> Seriously I would like you to add something in your PEP that addresses
> this simple alternative and explain why you favour yours.  True,
> `i in range(len(sequence))' doesn't look very pleasing, but
> `i in indici(sequence)' does. And as an added bonus it's
> very similar to `(i, item) in indexed(sequence)'.
>
>
> > [Carel Fellinger]

Yes!

There is a pair of ideas that are intuitive, clean, and routinely useful:
   for i in indici(seqn)
   for i, item in indexed(seqn)

Both ideas could be implemented as generators and work sequencially
off the iter() of the sequence.  This will accommodate lazy evaluation,
low memory use, objects without a __len__ property, xrange objects,
file objects, and objects with sequencial access defined by __getitem__
-- basically anything that could be used by a 'for item in sequence'.

It may be that 'sequence' isn't the best choice of words.  Iter() takes
a 'collection' and 'for' takes an iterable.  No matter what you call
it, the pair of ideas looks like a great general purpose solution.

I would add one little wrinkle.  Define the functions to have an
optional starting point.  def indici(seqn, start=0).
This will accommodate the common case of using natural numbers
(starting from 1) or less common cases for those who start their
check numbers at 1001.

Let's implement those two ideas, cross-off the todo list
(loop counter iteration proposals), then declare victory and
get on with life.


Raymond Hettinger

"In theory, there is no difference between theory
and practice.  In practice, there is."





More information about the Python-list mailing list