PEP 284, Integer for-loops

Michael Chermside mcherm at destiny.com
Sun Apr 7 12:23:43 EDT 2002


Greg Ewing writes:
> To me, the lack of a step is a *feature*, not a bug!
> 
> Steps other than 1 in a range are confusing when
> you don't land exactly on the endpoints. Which
> endpoint do you miss? Do you even hit either of
> them? And it's even worse when the step is negative.
> Do the endpoints swap over? Does the closed/open
> status of the endpoints swap over? Do you miss
> the opposite endpoint if the step isn't -1?
> Etc.
> 
> So, I see *considerable* benefit from eliminating
> this degree of freedom from the range generation
> itself, and making the programmer be explicit on
> how to map a step-1 range onto something else if
> needed.

I agree. If you want a "generalized" loop, with the ability to have 
varied step size, forward and backward looping, flexible exit or start 
conditions (inclusive, exclusive, apply-arbitrary-test), or even 
complicated things like:

John Machin writes:
 > for k in 0,1,2, 5 step 5 until 25, k * 2 while k <=1024:

Then there's a PERFECT control structure for your purpose. It's called a 
"while" loop, and it requires you to be explicit, but is otherwise 
completely flexible.

Seems to me that the key problem to solve is the awkwardness of 
addressing the common case -- simplicity is nice!

-- Michael Chermside






More information about the Python-list mailing list