Python 内建函数 - eval(expression, globals=None, locals=None)

Python 的内置函数 `eval()` 用于执行一个字符串表达式,并返回表达式的值。它接受一个字符串参数和可选的全局及局部字典,用于解析和执行表达式。如果全局字典缺少 'builtins',当前全局环境会被复制到全局字典中。`eval()` 可用于执行代码对象,当模式为 'exec' 时,返回值为 `None`。注意安全问题,可以使用 `ast.literal_eval()` 安全地评估只包含字面量的表达式。
摘要由CSDN通过智能技术生成

Manual

The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object.

The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is present and lacks ‘builtins’, the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard builtins module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值