Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to modify locals dictionary?

Tags:

locals is a built in function that returns a dictionary of local values. The documentation says:

Warning

The contents of this dictionary should not be modified; changes may not affect the values of local variables used by the interpreter.

Unfortunately, exec has the same problem in Python 3.0. Is there any way round this?

Use Case

Consider:

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

depends stores the strings provided in its arguments in a list test.dependences. These strings are keys in a dictionary d. I would like to be able to able to write put_into_locals so that we could pull the values out of d and put them into the locals. Is this possible?

like image 206
Casebash Avatar asked Sep 20 '09 04:09

Casebash


1 Answers

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.

like image 56
Unknown Avatar answered Oct 13 '22 00:10

Unknown