Thoughts on PEP284

Sean Ross sross at connectmail.carleton.ca
Mon Sep 22 22:41:04 EDT 2003


"Stephen Horne" <$$$$$$$$$$$$$$$$$@$$$$$$$$$$$$$$$$$$$$.co.uk> wrote in
message news:5j4vmvgnr5v68f6lupbpgt0mop6l7ps2sn at 4ax.com...
>
> Some more looping thoughts - this time on integer for loops...

Hi.
How about the following:

for i in 0...10:
    # suite

where 0...10 works something like xrange(10). So you get i=0 then 1 then 2
... then 9. To get 0 thru 10, inclusive, you'd need to say 0...11 (Ruby has
0..10, but I doubt that'd pass muster here).  This suggestion,
unfortunately, does not provide step control and I can't think of a clean
way to introduce it other than via keyword:

for i in 0...10 by 2:
    # suite

which will also be shot down, I suspect, as would

for i in 0 to 10 by 2:

for i in 0 thru 10 by 2:

etc.

Perhaps generators could grow a by() method so that you could control how
you step thru the iteration, something like this:

for i in 0...10.by(2):
    # suite

Whatever.

Leaving the ellipsis aside, you could add methods to int/long (also ala
Ruby),

for i in 0.upto(10):
    # suite

# to count down
for i in 10.downto(0):
    # suite

where the methods upto() and downto() are generators with an optional step
argument, i.e.

def upto(self, stop, step=1):
    # suite


And, after all of that, you could say, well we already have

for i in range(10):
    # suite

If only range was a generator function...but that'd break code, and you can
use xrange(), and some day it will(may) be, and ..., and ..., yada, yada,
yada.

Whatever.






More information about the Python-list mailing list