解析字符串表达式并执行,并返回一个值
eval(expression[, globals[, locals]])
print(eval("123"))
print(eval("True"))
print(eval("(1,2,3)"))
print(eval("[1,2,3]"))
# 输出结果
123
True
(1, 2, 3)
[1, 2, 3]
print(eval("1+2"))
x = 1
print(eval('x+1'))
# 输出结果
3
2
a = 1
b = 2
print(eval("[a,b]"))
# 输出结果
[1, 2]
# 使用 globals
x = 10
g = {"x": 5}
print(eval("x+1", g))
# 输出结果
6
在 eval 中提供了globals 参数
eval 的作用域就是 g 指定的这个字典,外面的 x = 10 被屏蔽掉了,eval 是看不见的,所以使用了 x 为 5 的值
x = 10
y = 5
g = {"x": 5}
print(eval("x+1+y", g))
# 输出结果
5
print(eval("x+1+y", g))
File "<string>", line 1, in <module>
NameError: name 'y' is not defined
因为 global 参数没有 y 变量值,所以报错了
# 使用 locals
a = 1
g = {"a": 2, "b": 3}
l = {"b": 30, "c": 4}
print(eval("a+b+c", g, l))
# 输出结果
36
# 字符串转字典
jsons = "{'a':123,'b':True}"
print(type(eval(jsons)))
# 输出结果
<class 'dict'>
print(eval("{'name':'linux','age':age}", {"age": 123}))
# 输出结果
{'name': 'linux', 'age': 123}
print(eval("{'name':'linux','age':age}", {"age": 123}, {"age": 24}))
# 输出结果
{'name': 'linux', 'age': 24}
# 内置函数
print(eval("dir()"))
print(eval("abs(-10)"))
# 输出结果
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'g', 'jsons', 'l', 'x', 'y']
10
print(eval("aa"))
# 输出结果
print(eval("aa"))
File "<string>", line 1, in <module>
NameError: name 'aa' is not defined
print(eval("[a,b,c]"))
# 输出结果
print(eval("[a,b,c]"))
File "<string>", line 1, in <module>
NameError: name 'c' is not defined
print(eval("if x: print(x)"))
# 输出结果
print(eval("if x: print(x)"))
File "<string>", line 1
if x: print(x)
^
SyntaxError: invalid syntax
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有