Dynamically set local variable [duplicate]

Posted on

Solving problem is about exposing yourself to as many situations as possible like Dynamically set local variable [duplicate] and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Dynamically set local variable [duplicate], which can be followed any time. Take easy to follow this discuss.

Dynamically set local variable [duplicate]

How do you dynamically set local variable in Python (where the variable name is dynamic)?

Asked By: Jonathan


Answer #1:

Contrary to other answers already posted you cannot modify locals() directly and expect it to work.

>>> def foo():
    lcl = locals()
    lcl['xyz'] = 42
>>> foo()
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
  File "<pyshell#5>", line 4, in foo
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

or if you prefer, use a class:

class C: pass
obj = C()
setattr(obj, 'xyz', 42)

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()
{'__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().

Answered By: Duncan

Answer #2:

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
>>> 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)?

Answered By: kindall

Answer #3:

(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
Answered By: ????

Answer #4:

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'
Answered By: poke

Answer #5:

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):
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:
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.

Answered By: Redsplinter

Answer #6:

You can use a local dictionary and put all the dynamic bindings as items into the dictionary. Then knowing the name of such a “dynamic variable” you can use the name as the key to get its value.

Answered By: Adam Zalcman

Answer #7:

Let’s say We have the below dictionary:

DictionaryA = {'No Rating': ['Hobbit', 'Movie C', 'Movie G'],
               'Forget It': ['Avenger', 'Movie B'],
               'Must See': ['Children of Men', 'Skyfall', 'Movie F'],
               '3': ['X-Men', 'Movie D'],
               '2': ['Captain America', 'Movie E'],
               '4': ['Transformers', 'Movie A']}

I want to create new dictionaries as below:

NewDictionary1 = {'No Rating': ['Hobbit', 'Movie C', 'Movie G']}
NewDictionary2 = {'Forget It': ['Avenger', 'Movie B']}
NewDictionary3 = {'Must See': ['Children of Men', 'Skyfall', 'Movie F']}

a oneliner:

dics = [{k:v} for k,v in DictionaryA.iteritems()]

would be outputted to:

[{'Must See': ['Children of Men', 'Skyfall', 'Movie F']}, {'Forget It': ['Avenger', 'Movie B']}, {'No Rating': ['Hobbit', 'Movie C', 'Movie G']}, {'3': ['X-Men', 'Movie D']}, {'2': ['Captain America', 'Movie E']}, {'4': ['Transformers', 'Movie A']}]

But to precisely declaring variables we could go with:

>>> i=0
>>> lcl = locals()
>>> for key,val in DictionaryA.iteritems():
        lcl["Dict" + str(i)] = {key:val}
        i += 1

As can be seen the first 3 Dict variables:

>>> Dict0
{'Must See': ['Children of Men', 'Skyfall', 'Movie F']}
>>> Dict1
{'Forget It': ['Avenger', 'Movie B']}
>>> Dict2
{'No Rating': ['Hobbit', 'Movie C', 'Movie G']}

As mentioned by others, if you want to put it in a function you should add it to the globals():

>>> glb = globals()
>>> for key,val in DictionaryA.iteritems():
        glb["Dict" + str(i)] = {key:val}
        i += 1
Answered By: 0x90
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.