[Python-Dev] Possible resolution of generatorexpressionvariablecapture dilemma

Guido van Rossum guido at python.org
Thu Mar 25 19:01:38 EST 2004


> With regard to generator expressions, isn't Guido leaning
> back towards not having any variable capture?

Right.

> Or is he un-leaning-back again? This is all getting very
> confusing...

Paul Dubois told me he thought that an example I used in my keynote
today (which would fail without capture) would be an attractive
nuisance for scientists (since it looked just like a reasonable way to
compute an outer product).  I'm not sure the argument holds, because
an outer product would typically be created as a list of lists, not a
list of generators.

The example I used was:

F = []
for i in range(10):
    F.append(x*i for x in range(10))

It would end up creating 10 generators referencing the variable i with
value 9.

The version using list comprehensions of course works:

F = []
for i in range(10):
    F.append([x*i for x in range(10)])

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



More information about the Python-Dev mailing list