Python 'for' loop is memory inefficient

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
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 ix.netcom.com> 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
...
0
1
2
Surprise!
4
>>> __builtins__.range is range
True


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.)



-- 
Steven



More information about the Python-list mailing list