Python scope is too complicated

Terry Reedy tjreedy at udel.edu
Mon Mar 21 15:16:18 EST 2005


"jfj" <jfj at freemail.gr> wrote in message news:423F4C76.50108 at freemail.gr...
> def foo(x):
>      y= (i for i in x)
>      return y
>
> From the disassembly it seems that the generator is a code object

What is type(foo([1,2,3])) ?

> but 'x' is not a cell variable. WTF?

As I understand it, the object 'x' binds to is immediately used to create 
the generator object.  The local name is just a dummy that is not part of 
the result.  I believe that the above is equivalent to

def foo(x):
  def _(z):
    for i in z: yield i
  return  _(x)

although I suspect that the implementation builds the generator more 
directly.

Terry J. Reedy






More information about the Python-list mailing list