using range() in for loops

John Salerno johnjsal at NOSPAMgmail.com
Tue Apr 4 21:54:09 EDT 2006


I'm reading Text Processing in Python right now and I came across a 
comment that is helping me to see for loops in a new light. I think 
because I'm used to the C-style for loop where you create a counter 
within the loop declaration, for loops have always seemed to me to be 
about doing something a certain number of times, and not about iterating 
over an object.

The reason for this distinction comes from the fact that I read a lot 
how using range and for is somewhat discouraged, because it doesn't 
really use a for loop for it's true purpose. So my question is, is this 
just a Python-oriented opinion about for loops, or is it a general idea?

Also, what if you *do* need to just do something a set number of times. 
Is this okay, or does it mean you are approaching the problem 
incorrectly? Using for and range together seems to be a common idiom, 
yet at the same time discouraged, so I'm wondering what is a good balance.

Thanks.



More information about the Python-list mailing list