[Python-ideas] Pass a function as the argument "step" of range()

Steven D'Aprano steve at pearwood.info
Sat Jul 4 05:58:39 CEST 2015

On Thu, Jul 02, 2015 at 10:20:16PM +0200, Pierre Quentel wrote:

> Both stress that range is most often used in a for loop (it doesn't
> "happens to sometimes be used" in for loops, and is rarely used for
> membership testing). 

You have misunderstood me. I'm not saying that range necessarily has 
many widespread and common uses outside of for-loops, but that for-loops 
only sometimes use range. Most loops iterate directly over the iterable, 
they don't use a range object at all.

You started this thread with an example from Javascript. For loops in 
Javascript can be extremely general:

js> for(var i=1,j=0,k=2; i < 100; j=2*i, k-=1, i+=j+k){print([i,j,k])}

Why try to force all that generality into the range function?

There are really two issues here:

(1) Is there a problem with Python that it cannot easily or reasonable 
perform certain for-loops that Javascript makes easy?

(2) Is modifying range() the right way to solve that problem?

I don't think you have actually demonstrated the existence of a problem 
yet. True, Javascript gives you a nice, compact, readable syntax for 
some very general loops, but Python has its own way of doing those same 
loops which may not be quite as compact but are probably still quite 

But even if we accept that Javascript for-loops are more powerful, more 
readable, and more flexible, and that Python lacks an acceptable way of 
performing certain for-loops that Javascript makes easy, changing 
range does not seem to be the right way to fix that lack.


More information about the Python-ideas mailing list