[Python-Dev] accumulator display syntax

Phillip J. Eby pje at telecommunity.com
Tue Oct 21 19:13:43 EDT 2003


At 03:14 PM 10/21/03 -0700, Guido van Rossum wrote:

>[Samuele]
> > 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
>
>Argh.  Of course.
>
>No, I think it should use the actual value of y, just like a nested
>function.

Why?


>Never mind that idea then.

Actually, I consider Samuele's example a good argument in *favor* of the 
idea.  Because of the similarity between listcomps and generator 
expressions (gen-X's? ;) )  it seems late binding of locals would lead to 
people thinking the behavior is a bug.  Since a genex is not a function (at 
least in form) a late binding would be very non-obvious and 
counterintuitive relative to other kinds of expressions.




More information about the Python-Dev mailing list