Is current integer for-loop syntax a wart?

Graham Breed usenet at microtonal.co.uk
Fri Mar 8 16:59:00 EST 2002


Cromwell, Jeremy wrote:

> PEP 276 (Simple Iterator for ints) and PEP 284 (Integer for-loops), 
> among
> others, are written from the perspective that the current integer 
> for-loop
> syntax
> 
>     for i in range(10):
> 
> is a wart.  Is this true?  That is, where does the Python community 
> stand on
> this issue?

That's more than syntax.  The "integer" bit is handled by the range 
builtin, which works fine outside of for loops.  The PEPs seem to be 
getting round problems with range, not problems with for loops.  So if 
we're strictly talking about for loops,

> -Not a wart
> This is the most pythonic way to write an integer for loop.  I would 
> oppose
> almost any change.

In fact I'll go further -- for loops are one of the most beautiful 
features of the language, precisely because there's no special syntax for 
integers.  Such an obvious idea, so many languages miss it.

Now, the range() builtin I see as a minor wart.  It certainly does look 
ugly for algorithms that require a closed set of integers to loop over.  
In the example above it works fine.  If somebody can come up with a 
perfect range literal so be it.  If not, indices() and inclusive() will do 
me.

                              Graham




More information about the Python-list mailing list