more fun with PEP 276
David Eppstein
eppstein at ics.uci.edu
Mon Dec 3 22:03:57 EST 2001
In article <mailman.1007424313.28420.python-list at python.org>,
James_Althoff at i2.com wrote:
> Let's start with Greg Ewing's recent suggestion of writing for-loops as:
>
> for -5 <= i <= 5:
> print i
>
> The nice thing about the above is the apparent clarity of intent. And the
> fact that all combinations of open and closed intervals are handled nicely.
> On the down side we observe that this construct requires new syntax, that
> it doesn't work outside of the context of a for-loop (in fact, it is a
> relational expression outside the context of a for-loop), and that there is
> no apparent mechanism for having a step size other than 1 (or -1).
But there isn't a huge distinction between "works only in a for loop" and
"produces general-purpose iterators" because of list comprehensions:
L = [i for -5 <= i <= 5]
--
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