for a in range(1000000) :

Maurix maurix78_remove_this_ at wanadoo.es
Sun Jun 15 11:04:35 EDT 2003


Hi everyone,
When i start learning python (some months ago) and for the first time i 
seed the "for a in range" structure i thought, what a stupid form: use 
so many memory for nothing: the range function have to allocate a list 
useless.
Now i huderstand the power of for sentence but still no huserstand the 
use of range function when you don't want a list but only an indexable 
object, look at this code:

class ran :
    def __init__(self,len) :
       self.len=len
    def __getitem__(self,i) :
       if i>=self.len :
         raise IndexError
       else :
         return i
    def __len__(self) :
       return self.len

for a in ran(1000000):
    pass

Exist someting like this in python library? Why not all the people use 
this form an not range() that allocate memory? Maybe may questions are 
stupid but i'm a newbie in python, sorry.

Thanks.
Maurix





More information about the Python-list mailing list