Re: Is using range() in for loops really Pythonic?
Matt Nordhoff wrote:
CPython really is naive. That sort of thing should be a
compile-time optimization.
John Nagle
Matt Nordhoff wrote:
John Salerno wrote:
>
Well, you should use "xrange(10) " instead of "range(10)" .
>I know it's popular and very handy, but I'm curious if there are purists
>out there who think that using something like:
>>
>for x in range(10):
> #do something 10 times
>>
>is unPythonic. The reason I ask is because the structure of the for loop
>seems to be for iterating through a sequence. It seems somewhat
>artificial to use the for loop to do something a certain number of
>times, like above.
>>
>Anyone out there refuse to use it this way, or is it just impossible to
>avoid?
>out there who think that using something like:
>>
>for x in range(10):
> #do something 10 times
>>
>is unPythonic. The reason I ask is because the structure of the for loop
>seems to be for iterating through a sequence. It seems somewhat
>artificial to use the for loop to do something a certain number of
>times, like above.
>>
>Anyone out there refuse to use it this way, or is it just impossible to
>avoid?
Well, you should use "xrange(10) " instead of "range(10)" .
compile-time optimization.
John Nagle
Comment