Is there a way in Python to add to the locals name-space by calling a function without explicitly assigning variables locally?
Something like the following for example (which of course doesn't work, because locals() return a copy of the local name-space) where the print statement would print '1'.
def A():
B(locals())
print x
def B(d):
d['x'] = 1
Seems pretty horrible to rely on a hack like
exec ''
. What about communicating like this with the global statement, it seems to work:You could create a namespace for your variables instead:
Then use
names.x
orgetattr(names, "x")
to access the attributes.In Python
2.*
, you can disable the normal optimizations performed by the Python compiler regarding local variable access by starting your function withexec ''
; this will make the function very much slower (I just posted, earlier today, an answer showing how the local-variable optimization can easily speed code up by 3 or 4 times), but it will make your desired hack work. I.e., in Python 2.*:will emit
1
, as you desire.This hack was disabled in Python
3.*
(whereexec
is just a function, not a statement nor a keyword any more) -- the compiler now performs the local variable optimization unconditionally, so there is no longer any way to work around it and make such hacks work.