使用__dict__:>>> def foo():... pass... >>> a = "hello">>> foo.__dict__[a] = "world">>> foo.hello'world'globals() 对于全局变量(返回全局变量字典):>>> name = "may">>> globals()[name] = "the force be with you">>> may'the force be with you'locals() 对于局部变量:>>> name = "life">>> locals()[name] = "was like a box of chocolates">>> life'was like a box of chocolates'