[Python-Dev] accumulator display syntax

Samuele Pedroni pedronis at bluewin.ch
Tue Oct 21 17:33:30 EDT 2003


At 14:04 21.10.2003 -0700, Guido van Rossum wrote:
> > [Guido]
> > > ...
> > > 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).
>
>[Tim]
> > The implementation could synthesize a generator function abusing default
> > arguments to give the generator's frame locals with the same names.
>
>Yes, I think that could work -- I see no way that something invoked by
>the generator expression could possibly modify a variable binding in
>the surrounding scope.

so this, if I understand:

def h():
   y = 0
   l = [1,2]
   it = (x+y for x in l)
   y = 1
   for v in it:
     print v

will print 1,2 and not 2,3

unlike:

def h():
   y = 0
   l = [1,2]
   def gen(S):
     for x in S:
       yield x+y
   it = gen(l)
   y = 1
   for v in it:
     print v









More information about the Python-Dev mailing list