Python 'for' loop is memory inefficient

Steven D'Aprano steve at
Mon Aug 17 15:23:14 CEST 2009

On Sun, 16 Aug 2009 15:35:26 -0700, sturlamolden wrote:

> On 16 Aug, 14:57, Dennis Lee Bieber <wlfr... at> wrote:
>>         Well, the alternative would be to have two keywords for
>>         looping: one
>> for your "simple" incrementing integer loop, and another for a loop
>> that operates over the elements of some collection type.
> A compiler could easily recognise a statement like
>    for i in range(n):
> as a simple integer loop. 

"Easily" huh? Would you like to put a small wager on that?

Here is an unedited copy-and-paste of the last few lines of an 
interactive Python session:

>>> for i in range(5):
...     print i
>>> __builtins__.range is range

Can you determine how I did this? How would the compiler avoid this? If 
you can find a method for the compiler to avoid surprises like this, why 
do you think it would be valuable to add all that extra complexity to the 
language? (As opposed to an external tool like Cython.)


More information about the Python-list mailing list