efficiency of range() and xrange() in for loops
Carl Banks
invalidemail at aerojockey.com
Thu Apr 6 01:08:29 EDT 2006
Steve R. Hastings wrote:
> I wondered if the Python
> compiler could, as a special case, turn:
>
> for i in range(n)
>
> into compiled code equivalent to
>
> for i in itr
>
> where "itr" is a lightweight iterator that returns the same values as
> iter(range(n)).
Nope, out of the question for Python 2.x. Note that the the builtin
range could be rebound, or a global range could appear in the module,
at run time. There might even be a good reason to do so.... Point is,
optimizing the call to range can break compatibility even in the for
loop.
Carl Banks
More information about the Python-list
mailing list