Dave Angel d at davea.name
Thu Jul 5 00:48:08 CEST 2012

On 07/04/2012 07:56 AM, eisoab at gmail.com wrote:
> I expected this to work:
> def f(**args):
>     locals().update(args)
>     print locals()
>     print a
> d=dict(a=1)
> f(**d)
> but:
>> global name 'a' is not defined
> Where is my mistake?

Chris has given you the place where it's documented that it generally
won't work.  I'll try to explain why, at least for local variables
inside functions, and for CPython implementation.

The generated byte code for a function does *not* look up each symbol by
name during execution.  The compiler builds  a list of known local
symbol names, and gives them each an index (a small positive integer). 
At run time that list is fixed in size and meaning;  no new locals can
be added.  The locals() function just creates a dictionary that
approximates what the semantics of the symbols are.  But changes to that
dictionary have no effect on the running function.

That's one reason that copying a global value to a local symbol inside a
function can make that function run faster.  Globals always require a
dictionary lookup, while function locals are just an integer offset away.

> This does work:
> globals().update({'a':1})
> print a
> 1
> -E

As i said above, the rules are different for globals, and they are even
if you use locals() to access them.  However, i'd consider it prudent
never to assume you can write to the dictionary constructed by either
the locals() or the globals() functions.



More information about the Python-list mailing list