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

James_Althoff at i2.com James_Althoff at i2.com
Fri Mar 1 18:37:09 EST 2002


[Carel Fellinger]
> But even after that nice math lesson I still don't *understand* it,
> you iterate over a sequence and I have a hard time seeing an integer
> as a sequence.  Same for a length. And I believe many would have the
> same problem.

You're right; apparently they do.  Here's how I think of it.  Iterators are
really useful.  When asked to do so they produce items one after another
according to some meaningful plan.  You can associate an iterator with
anything that you think is practical and useful.  So, is there any reason
to associate an iterator with an integer?  PEP 276 suggests that because
there are times when you have the length of an indexed-collection and you
want to iterate over the indices of that collection it would be convenient
to associate an iterator with that "length" number.  It just makes it easy
and uncluttered to do
    for rowindex in table.rowcount:
        process(rowindex)

That's it.  You don't have to think of integer n as a sequence.  You just
need to remember that n can produce the iteration 0,1,2,...,n-1.

Perhaps it might help to change one's perspective of "for x in y" from
  "for each object that is a member of y, an ordered aggregation of other
things"
to
  "for each object that is produced by the iterator associated with y"

Works for me, but, clearly not everyone. :-)


>Besides, I don't see the huge advantage of this compared to
>`range(integer)' or `indici(sequenceORmapping)'.

If you are happy with range and xrange, then PEP 276 won't appeal to you.


> What about a function that accepts as argument such a collection
> and then generates the indici?  Much easier to explain, to understand,
> to adopt. But admitted, it takes a little extra typing.
>
> 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)'.

This has been suggested and is fine for one-dimensional,
indexed-collections that are written in Python and obey the standard
protocol for sequences -- specifically that they implement __len__.  But
how is such a function going to work for a two-dimensional Java table
(accessed via Jython)?  PEP 276 is more general.  And doesn't preclude such
a function being added, in any case.


> But I do think that dismissing our point as a matter of
> preference misses our point.

The PEP says this:

    - Some feel that iterating over the sequence "0, 1, 2, ..., n-1"
      for an integer n is not intuitive.  "for i in 5:" is considered
      (by some) to be "non-obvious", for example.

How does that miss your point?  What should it say?


> looking forward to your views on meta programming though

Thanks.


Jim





More information about the Python-list mailing list