Dynamically set local variable [duplicate]
How do you dynamically set local variable in Python (where the variable name is dynamic)?
Contrary to other answers already posted you cannot modify locals()
directly and expect it to work.
>>> def foo():
lcl = locals()
lcl['xyz'] = 42
print(xyz)
>>> foo()
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
foo()
File "<pyshell#5>", line 4, in foo
print(xyz)
NameError: global name 'xyz' is not defined
Modifying locals()
is undefined. Outside a function when locals()
and globals()
are the same it will work; inside a function it will usually not work.
Use a dictionary, or set an attribute on an object:
d = {}
d['xyz'] = 42
print(d['xyz'])
or if you prefer, use a class:
class C: pass
obj = C()
setattr(obj, 'xyz', 42)
print(obj.xyz)
Edit:
Access to variables in namespaces that aren't functions (so modules, class definitions, instances) are usually done by dictionary lookups (as Sven points out in the comments there are exceptions, for example classes that define __slots__
). Function locals can be optimised for speed because the compiler (usually) knows all the names in advance, so there isn't a dictionary until you call locals()
.
In the C implementation of Python locals()
(called from inside a function) creates an ordinary dictionary initialised from the current values of the local variables. Within each function any number of calls to locals()
will return the same dictionary, but every call to locals()
will update it with the current values of the local variables. This can give the impression that assignment to elements of the dictionary are ignored (I originally wrote that this was the case). Modifications to existing keys within the dictionary returned from locals()
therefore only last until the next call to locals()
in the same scope.
In IronPython things work a bit differently. Any function that calls locals()
inside it uses a dictionary for its local variables so assignments to local variables change the dictionary and assignments to the dictionary change the variables BUT that's only if you explicitly call locals()
under that name. If you bind a different name to the locals
function in IronPython then calling it gives you the local variables for the scope where the name was bound and there's no way to access the function locals through it:
>>> def foo():
... abc = 123
... lcl = zzz()
... lcl['abc'] = 456
... deF = 789
... print(abc)
... print(zzz())
... print(lcl)
...
>>> zzz =locals
>>> foo()
123
{'__doc__': None, '__builtins__': <module '__builtin__' (built-in)>, 'zzz': <built-in function locals>, 'foo': <function foo at 0x000000000000002B>, '__name__': '__main__', 'abc': 456}
{'__doc__': None, '__builtins__': <module '__builtin__' (built-in)>, 'zzz': <built-in function locals>, 'foo': <function foo at 0x000000000000002B>, '__name__': '__main__', 'abc': 456}
>>>
This could all change at any time. The only thing guaranteed is that you cannot depend on the results of assigning to the dictionary returned by locals()
.
Others have suggested assigning to locals()
. This won't work inside a function, where locals are accessed using the LOAD_FAST
opcode, unless you have an exec
statement somewhere in the function. To support this statement, which could create new variables that are not known at compile time, Python is then forced to access local variables by name within the function, so writing to locals()
works. The exec
can be out of the code path that is executed.
def func(varname):
locals()[varname] = 42
return answer # only works if we passed in "answer" for varname
exec "" # never executed
func("answer")
>>> 42
Note: This only works in Python 2.x. They did away with this foolishness in Python 3, and other implementations (Jython, IronPython, etc.) may not support it either.
This is a bad idea, though. How will you access the variables if you don't know their name? By locals()[xxx]
probably. So why not just use your own dictionary rather than polluting locals()
(and taking the chance of overwriting a variable your function actually needs)?
(Just a quick note for others googlin')
Ok, so modifying locals()
is not the way to go ( while modifying globals()
is supposed to work). In the meantime, exec
could be, but it's painfully slow, so, as with regular expressions, we may want to compile()
it first:
# var0 = 0; var1 = 1; var2 = 2
code_text = '\n'.join( "var%d = %d" % (n, n) for n in xrange(3) )
filename = ''
code_chunk = compile( code_text, filename, 'exec' )
# now later we can use exec:
exec code_chunk # executes in the current context
You could modify locals()
directly:
locals()['foo'] = 'bar'
But a better way would be to have some dict that holds all your dynamic variable names as dictionary keys:
d = {}
for some in thing:
d[some] = 'whatever'
I've spent the last... couple hours, I guess, trying to hack around the lack of function closures, and I came up with this, which might help:
common_data = ...stuff...
def process(record):
...logic...
def op():
for fing in op.func_dict: # Key line number 1
exec(fing + " = op.func_dict[fing]") # Key line number 2
while common_data.still_recieving:
with common_data._lock:
if common_data.record_available:
process(common_data.oldest_record)
time.sleep(1.0)
op.func_dict.update(locals()) # Key line number 3
threading.Thread(target = op).start()
...
It's a pretty heavy handed / contrived example, but if there are a lot of locals or you're still in the process of prototyping this pattern becomes useful. Mostly I was just bitter about all the data stores being replicated or moved in order to handle callback delegates, etc.