I'll keep this one short and simple. Why does the following behaviour occur?
>>> globals()['x'] = 5
>>> x
5
>>> def f():
... locals()['y'] = 7
... y
...
>>> f()
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
f()
File "<pyshell#31>", line 3, in f
y
NameError: name 'y' is not defined
Here's an example of where this might be used:
import opcode
def foo():
locals().update(opcode.opmap)
#do stuff