Any way to modify locals dictionary? Any way to modify locals dictionary? python python

Any way to modify locals dictionary?


I just tested exec and it works in Python 2.6.2

>>> def test():...     exec "a = 5"...     print a...>>> test()5

If you are using Python 3.x, it does not work anymore because locals are optimized as an array at runtime, instead of using a dictionary.

When Python detects the "exec statement", it will force Python to switch local storage from array to dictionary. However since "exec" is a function in Python 3.x, the compiler cannot make this distinction since the user could have done something like "exec = 123".

http://bugs.python.org/issue4831

To modify the locals of a function on the fly is not possible without several consequences: normally, function locals are not stored in a dictionary, but an array, whose indices are determined at compile time from the known locales. This collides at least with new locals added by exec. The old exec statement circumvented this, because the compiler knew that if an exec without globals/locals args occurred in a function, that namespace would be "unoptimized", i.e. not using the locals array. Since exec() is now a normal function, the compiler does not know what "exec" may be bound to, and therefore can not treat is specially.


The local variables are modified by assignment statements.

If you have dictionary keys which are strings, please don't also make them local variables -- just use them as dictionary keys.

If you absolutely must have local variables do this.

def aFunction( a, b, c, d, e, f ):    # use a, b, c, d, e and f as local variablesaFunction( **someDictWithKeys_a_b_c_d_e_f )

That will populate some local variables from your dictionary without doing anything magical.


This isn't possible. I think this is to allow for performance optimizations later on. Python bytecode references locals by index, not by name; if locals() was required to be writable, it could prevent interpreters from implementing some optimizations, or make them more difficult.

I'm fairly certain you're not going to find any core API that guarantees you can edit locals like this, because if that API could do it, locals() wouldn't have this restriction either.

Don't forget that all locals must exist at compile-time; if you reference a name that isn't bound to a local at compile-time, the compiler assumes it's a global. You can't "create" locals after compilation.

See this question for one possible solution, but it's a serious hack and you really don't want to do that.

Note that there's a basic problem with your example code:

@depends("a", "b", "c", "d", "e", "f")def test():    put_into_locals(test.dependencies)

"test.dependencies" isn't referring to "f.dependencies" where f is the current function; it's referencing the actual global value "test". That means if you use more than one decorator:

@memoize@depends("a", "b", "c", "d", "e", "f")def test():    put_into_locals(test.dependencies)

it'll no longer work, since "test" is memoize's wrapped function, not depends's. Python really needs a way to refer to "the currently-executing function" (and class).