are free variables part of the local dict ?

Michael Hudson mwh at python.net
Tue Jan 15 09:54:14 EST 2002


"Cesar Douady" <cesar.douady at asim.lip6.fr> writes:

> in :
> 
> def foo():
>     x=1
>     def bar():
>         x
>         print locals()
>     bar()
> foo()
> 
> the result is "{}", indicating that x is not in locals(). But in :
> 
> def foo():
>     x=1
>     def bar():
>         x
>         y=1
>         print locals()
>     bar()
> foo()
> 
> the result is "{'y':1, 'x':1}", indicating that the presence of y has made
> x part of locals().

Heh, wierd!  That certainly looks broken to me.

> My comprehension is that free variables should never be part of
> locals()

It doesn't seem to be clear from the PEP or the docs, but I thing this
is the more plausible interpretation.

> and that the second output is a bug. However, from looking at the
> interpreter code, it seems that this is done on purpose (i.e. free
> vars are part of the local dict only for functions, the difference
> between the first 2 codes still looks like a bug).

If you've looked at the code, can you see the bug?

> Before sending a bug report on such a tricky subject, I would appreciate
> getting various opinions.

File a bug and assign it to Jeremy.  Gets my vote anyway.

Cheers,
M.

-- 
  CLiki pages can be edited by anybody at any time. Imagine the most
  fearsomely comprehensive legal disclaimer you have ever seen, and
  double it                        -- http://ww.telent.net/cliki/index



More information about the Python-list mailing list