eval(expression, globals=None, locals=None),将字符串str当成有效的表达式来求值并返回计算结果。
example:
a = 1
g = {'a':20}
eval("a+1",g)
result
21
x = 1
y = 1
num1 = eval("x+y")
print(num1)
def g():
x = 2
y = 2
num3 = eval("x+y")
print(num3)
num2 = eval("x+y", globals())
num5 = eval("x+y",globals(),locals())
print(num2)
print(num5)
g()
result
2
4
2
4