[Python-Dev] Possible resolution of generator expression variable capture dilemma

Guido van Rossum guido at python.org
Wed Mar 24 11:24:43 EST 2004


Phillip shows some examples involving inner functions defined inside
the for loop.  But in current Python these examples work just as well
if the function is defined *outside* the loop (as long as no default
argument values are needed):

> for x in 1,2,3:
>      def y():
>          print z
>      z = x * 2
>      y()

is the same as

  def y():
      print z
  for x in 1, 2, 3:
      z = x*2
      y()

That would change under Phillip's proposal.

There are similar examples that would break under Greg's original
proposal: just use the loop variable in the function, e.g.:

  def y():
      print x*2
  for x in 1, 2, 3:
      y()

All this is just more reason to put this one off until 3.0.

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



More information about the Python-Dev mailing list