Is using range() in for loops really Pythonic?

John Salerno johnjsal at NOSPAMgmail.com
Sun May 11 23:46:42 EDT 2008


Ben Finney wrote:

> John Salerno <johnjsal at gmailNOSPAM.com> writes:
>
>> num = 33
>> 
>> for x in xrange(10):
>>     print num += 1
>
> Which is better done by 'num += 10'.
>
> Can you come up with an example that isn't trivially replaced with
> clearer code? That might make it clearer what your concern is.

::sigh:: No, unfortunately I don't have a strong enough grasp of Python
to give a really in-depth example. I understand what you're asking
though. Perhaps people don't use this idiom as much as I think they do,
so to give a trivial example to support my point isn't helpful.

As far as I know, though, I think this is used often enough, so I
thought I'd just ask if there are purists out there who don't like this
use of the loop and feel it's an abuse of the syntax.



More information about the Python-list mailing list