[Python-Dev] accumulator display syntax

Guido van Rossum guido at python.org
Tue Oct 21 16:46:42 EDT 2003


> Iterator expression?

Better.  Or perhaps generator expression?  To maintain the link with
generator functions, since the underlying mechanism *will* be mostly
the same.  Yes, I like that even better.

BTW, while Alex has shown that a generator function with no free
variables runs quite fast, a generator expression that uses variables
from the surrounding scope will have to use the nested scopes
machinery to access those, unlike a list comprehension; not only does
this run slower, but it also slows down all other uses of that
variable in the surrounding scope (because it becomes a "cell"
throughout the scope).

Someone could time how well

  y = 1
  sum([x*y for x in R])

fares compared to

  y = 1
  def gen():
     for x in R: yield y*y
  sum(gen())

for R in (range(N) for N in (100, 1000, 10000)).

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



More information about the Python-Dev mailing list