Domanda

Assigning a string, integer, and so on with globals() works fine:

 
>>> globals()
{'__builtins__': , '__name__': '__main__', '__doc__': None, '__package__': None}
>>> globals()["a"] = 5
>>> a
5
>>> globals()
{'__builtins__': , '__name__': '__main__', '__doc__': None, 'a': 5, '__package__': None}

However, trying to assign to a dictionary fails:

>>> globals()["b['c']"] = 5
>>> globals()
{'a': 5, "b['c']": 5, '__builtins__': , '__package__': None, '__name__': '__main__', '__doc__': None}
>>> b['c']
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'b' is not defined

This is true even if "b" is already defined as a dictionary.

So, given a text string such as "b['c']", how do I assign b['c']?

È stato utile?

Soluzione

I can't imagine what you're trying to do here.

b doesn't seem to exist in globals already. You can't assign to a dictionary that doesn't exist.

Conceivably, you could do this:

globals()["b"] = {'c': 5}

which makes b a new dictionary containing one key, c, with a value of 5. But I'd think carefully about why you think you need to modify globals in the first place - there's almost certainly a better way to do what you want.

Altri suggerimenti

The dictionary returned by globals may have a key for every global variable, but not every key in that dictionary needs to correspond to a global. Specifically, b["c"] is not a single global variable, but a syntactic structure for accessing the value that b associates with c. The following does work (but isn't necessarily recommended):

>>> b = {}
>>> globals()["b"]["c"] = 1
>>> b
{'c': 1}

How would you assign to a dictionary contained in a dictionary?

>>> outer = {'inner': { 'foo': 'bar' }}
>>> print outer['inner']['foo']
bar

>>> outer['inner']['foo'] = 'baz'
>>> print outer['inner']['foo']
baz

globals() just returns the dictionary in which global variables are stored. The variable names are the keys. So you access it (and any nested structures inside it) the same way you would with any other dictionary.

>>> globals()['outer']['spoon'] = 'fork'
>>> print outer['spoon']
'fork'

If you find this confusing, just break it up into one step at a time using more intermediate variables:

>>> g = globals()         # fetch the globals() dictionary into g
>>> o = g['outer']        # fetch the outer dictionary from g into o
>>> o['spoon'] = 'fork'   # store 'fork' under the key 'spoon' in o

Try to do that same "breaking up into smaller steps" with your attempted syntax, and you'll find that you get:

>>> g = globals()         # fetch the globals() dictionary into g
>>> g["b['c']"] = 5       # store 5 under the key "b['c']" in g

Here you've inserted a value with into a dictionary with the key "b['c']". Which is a perfectly fine thing to do with a dictionary, so you don't get any errors. But it's utter nonsense mapped back to variables.

What you've done corresponds to the creation of a variable with the name b['c']. Not a variable named b referring to a dictionary with a key 'c'. There is no way to actually refer to this variable without going through globals(), because that's not a valid Python identifier. Every time you try to write it out Python will just interpret it as you referring to a key inside a variable named b.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top