Re: Lazy lists for use with large dictionaries

Anton Hartl (hartl@Informatik.TU-Muenchen.DE)
29 Sep 1994 15:42:06 GMT

Guido.van.Rossum@cwi.nl writes:
>I understand the principle, and am thinking of adding it to the
>language a a replacement of the internals of the for loop. It would
>make it possible e.g. to say "for line in file.lines(): ...", to loop
>over dictionaries etc.

>However I don't understand how you can do it without creating *any*
>new objects -- surely two or more loops over the same sequence must be
>possible, so an object holding a pointer to the current item must be
>made. I also think dictionaries should know that one or more
>iterators are active so they can trap modifications to their key set.

Isn't the same true for lists also? The follwing will result in an
endless loop (well, almost, limited by memory):

list = [1,2,3,4]
for l in list:
list.append(5)

What do you have in mind with trapping modifictions? I don't see an
obvious way how to deal with that situation in general, i.e. iterating
over a set of elements while the set is modified.

>Efficiency in Python is a wonderful thing -- your intuition is usually wrong.
I guess than you have to correct the inituitions of us poor python users
from time to time, as you have done with your comment on my original posting. :)

-Toni