[Python-3000] range() issues

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Apr 30 11:24:18 CEST 2008


Alexander Belopolsky wrote:
> On Tue, Apr 29, 2008 at 10:36 PM, Guido van Rossum <guido at python.org> wrote:
> ..
>> R = range(N)
>> for i in R:
>>  for j in R:
>>    ....
> 
> You realize that in the snippet above whatever cycles you save by
> creating R once, you give away by creating iter(R) twice.

I'm not so sure about that. Evaluating range(N) involves
a global lookup and Python function call, whereas extracting
an iterator from a C-implemented iterable doesn't.

The difference is likely to be even greater when
there are more arguments involved, such as range(m, n, s).

Also, the range may be getting computed elsewhere. There
are notational conveniences to being able to wrap the
description of a range up into a single object
that can be passed around and iterated over easily.

-- 
Greg


More information about the Python-3000 mailing list