[Python-ideas] free variables in generator expressions
Georg Brandl
g.brandl at gmx.net
Wed Dec 12 22:56:35 CET 2007
Brett Cannon schrieb:
> On Dec 12, 2007 12:10 PM, Arnaud Delobelle <arno at marooned.org.uk> wrote:
>> In the generator expression
>>
>> (x+1 for x in L)
>>
>> the name 'L' is not local to the expression (as opposed to 'x'), I
>> will call such a name a 'free name', as I am not aware of an existing
>> terminology.
>>
>> The following is about how to treat 'free names' inside generator
>> expressions. I argue that these names should be bound to their values
>> when the generator expression is created, and the rest of this email
>> tries to provide arguments why this may be desirable.
>>
>
> Calling it a free variable is the right term (at least according to
> Python and the functional programmers of the world).
>
> As for what you are asking for, I do believe it came up during the
> discussion of when genexps were added to the language. I honestly
> don't remember the reasoning as to why we didn't do it this way, but I
> am willing to guess it has something to do with simplicity and purity
> of what genexps are meant to be.
>
> Consider what your genexp, ``(x for x in P if x % p)``, really is::
>
> def _genexp():
> for x in P:
> if x % p:
> yield x
Actually it is
def _genexp(P):
for x in P:
if x % p:
yield x
IOW, the outmost iterator is not a free variable, but passed to the
invisible function object.
Georg
More information about the Python-ideas
mailing list