Status of PEP's?

David Eppstein eppstein at ics.uci.edu
Thu Feb 28 23:56:34 EST 2002


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

> For example, one problem I had when playing with an experimental
> implementation of the "middle argument" variant mentioned above is that the
> specific relational operators are tied to the values of the bounds.  If you
> want to iterate from the smaller value (up) to the larger value you write:
> 
> for 1 <= var < 5:
> 
> whereas if you want to iterate from the larger value (down) to the smaller
> value you have to turn things around and write:
> 
> for 5 > var >= 1:
> 
> which all looks very nice when you are dealing with literal constants.  But
> suppose you have variables for the bounds, "start" and "end"?  Let's say I
> have a function
> 
> def doSomethingFromTo(start,end):
> 
> The function has to test to see which of "start" and "end" is the smaller
> value in order to know whether to execute a for-loop with "<" or a for-loop
> with ">" (and the code specified in each separate for-loop has to be
> written redundantly).  I found this to be awkward.

What's wrong with
    for min(start,end) < var < max(start,end)
?

I am assuming you want an open interval because you wrote < and > above.
If you want a closed interval, of course, use <=, but I don't think it 
makes sense to ask for a half-open interval when you don't know which end 
is which.
-- 
David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list