Status of PEP's?

David Eppstein eppstein at ics.uci.edu
Mon Mar 4 20:08:46 EST 2002


In article <mailman.1015286048.32329.python-list at python.org>,
 James_Althoff at i2.com wrote:

> Yes, I'll grant "is at least consistent".  But, in the case of the
> for-loop, at some point -- rather quickly I suspect -- you are going to
> have to leave the realm of relational operators and get right back to
> talking about iterators.  Because you're going to have to explain why
>     for x <= i <= y:
> works and why
>     for x <= i and i != y:
> is a syntax error.

For the same reason that
    for item in list
works but
    for dictionary.has_key(item)
doesn't:  The for-loop syntax only allows a very restricted set of 
expressions that it knows how to handle.  The current set consists only 
of expressions like "item in list".  The proposal is to extend it to 
also include three-way comparisons.

> And why
>     for i < rowcount:
> uses "i" as the loop variable instead of "rowcount" -- or does it?
>     for rowcount < i:
> How would Python decide?

It wouldn't -- that is not one of the expressions proposed to be allowed 
in a for loop.

> And if it can't, then does this mean that the idiom cannot support
> shortcuts for the common case of iterating the indices of a list?  So, do
> we have to write:
>     for 0 <= i < rowcount:
> specifying the "0 <=" part every time?

Yes.

One could extend the proposal to allow two-way comparisons that start at 
zero, but that wouldn't be in the spirit of the proposal, because your 
suggested "for i < rowcount" wouldn't loop over all those i for which "i 
< rowcount" is true (it would skip negative i).

And of course, there is no logical contradiction between the proposed 
new syntax and PEP 276, once one accepts that PEP 276 defines a meaning 
for the expression "i in 276" and not just the loop "for i in 276".



More information about the Python-list mailing list