我有一个分配了字符串的变量,我想根据该字符串定义一个新变量。
foo = "bar" foo = "something else" # What I actually want is: bar = "something else"
你可以使用exec:
>>> foo = "bar" >>> exec(foo + " = 'something else'") >>> print bar something else >>>