Adding to local namespace in Python?
问题 Is there a way in Python to add to the locals name-space by calling a function without explicitly assigning variables locally? Something like the following for example (which of course doesn't work, because locals() return a copy of the local name-space) where the print statement would print '1'. def A(): B(locals()) print x def B(d): d['x'] = 1 回答1: In Python 2.* , you can disable the normal optimizations performed by the Python compiler regarding local variable access by starting your