[Python-Dev] accumulator display syntax

Guido van Rossum guido at python.org
Fri Oct 17 18:47:28 EDT 2003

> Hm.  What if list comprehensions returned a "lazy list", that if you took 
> an iterator of it, you'd get a generator-iterator, but if you tried to use 
> it as a list, it would populate itself?  Then there'd be no need to ever 
> *not* use a listcomp, and only one syntax would be necessary.
> More specifically, if all you did with the list was iterate over it, and 
> then throw it away, it would never actually populate itself.  The principle 
> drawback to this idea from a semantic viewpoint is that listcomps can be 
> done over expressions that have side-effects.  :(

I don't think this can be done without breaking b/w compatibility.  Example:

  a = [x**2 for x in range(10)]
  for i in a: print i
  print a

Your proposed semantics would throw away the values in the for loop,
so what would it print in the third line?

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list