PEP 276 Simple Iterator for ints (fwd)
eppstein at ics.uci.edu
Mon Dec 10 20:41:30 CET 2001
In article <3C150188.C31AC4B8 at ccvcorp.com>,
Jeff Shannon <jeff at ccvcorp.com> wrote:
> (I still strongly dislike the proposed notation, though.... Maybe
> it's a style thing--I'd usually much rather have a clear function call
> than clever operator tricks.)
The two objections I have to the current "clear function call" (range and
friends) are first, you can't understand it unless you crack that manual
and have it tell you what the arguments mean and what the difference is
between range and xrange, which is no good for writing code that
non-Python-literate programmers can quickly and easily understand, and
second, even for Pythonists, it doesn't lead to clear code unless what you
need is half-open-on-the-right intervals with integer bounds, which only
cover some of the uses of range.
For example, suppose you want to loop over the set of integers i satisfying
x <= i < y (the usual half-open interval), but you need them in reverse
order, and x and y may be non-integer. As far as I can tell, the "one
right way" of doing this is
for i in range(int(math.ceil(y-1)),int(math.ceil(x-1)),-1): ...
Despite having some idea how range works I checked this in the interpreter
and had to correct multiple mistakes: forgot the "math.", misspelled "ceil"
as "ceiling", and incorrectly used floor(x+1) instead of ceil(x-1) in the
second argument. And there's still a possible bug when integer unification
comes around and makes long an acceptable argument for a range. This is a
"clear function call" compared to
for y > i >= x: ...
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