for loop: range() result has too many items

Matt Nordhoff mnordhoff at mattnordhoff.com
Tue Oct 13 23:32:39 EDT 2009


Matt Nordhoff wrote:
> Andre Engels wrote:
> [snip]
> 
>> However, I think that the better Python way would be to use a generator:
>>
>> def infinite_numbergenerator():
>>     n = 0
>>     while True:
>>          yield n
>>          n += 1
>>
>> for i in infinite_numbergenerator():
>>     ...
> 
> That's what itertools.count() is for.
> 
> <http://docs.python.org/library/itertools.html#itertools.count>
> 
> It'll be faster, too, since it's written in C. Not that it really
> matters, but it's always nice.

...Although itertools.count() is limited by sys.maxint (well,
PY_SSIZE_T_MAX), so I guess it isn't useful in this case.
-- 



More information about the Python-list mailing list