Is using range() in for loops really Pythonic?

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Mon May 12 03:15:36 EDT 2008


John Salerno schreef:
> 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.

Perhaps a function to print some text a number of times:

def print_n(text, n):
   for i in xrange(n):
     print text

(Though I guess you can replace that with print '\n'.join(msg * n) )

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
   -- Isaac Asimov

Roel Schroeven



More information about the Python-list mailing list