In python I have:
var = "abc"
How can I create a dictionary of name abc
using var
?
In python I have:
var = "abc"
How can I create a dictionary of name abc
using var
?
globals()
method returns a dictionary that contains all the names of global scope. You can use this to create a variable whose name is stored in another variable. Something like this:
>>> var = 'abc'
>>> globals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'var': 'abc', '__doc__': None, '__package__': None}
>>> globals()[var] = {}
>>> abc
{}
>>> globals()
{'abc': {}, '__builtins__': <module '__builtin__' (built-in)>, '__package__': None, 'var': 'abc', '__name__': '__main__', '__doc__': None}
>>> abc['p0'] = 2
>>> abc
{'p0': 2}
>>>
What do you mean? Assign var to a dictionary where 'name' evaluates to 'abc'? In that case,
var = {'name': 'abc'}
Otherwise, I have no idea what you're asking.