博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python内置函数(19)——eval
阅读量:4315 次
发布时间:2019-06-06

本文共 3436 字,大约阅读时间需要 11 分钟。

英文文档:

eval
(expression, globals=None, locals=None) 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 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 is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:
>>> x = 1>>> eval('x+1')2

This function can also be used to execute arbitrary code objects (such as those created by ). In this case pass a code object instead of a string. If the code object has been compiled with 'exec' as the mode argument, ‘s return value will be None.

Hints: dynamic execution of statements is supported by the function. The and functions returns the current global and local dictionary, respectively, which may be useful to pass around for use by or . See for a function that can safely evaluate strings with expressions containing only literals.
说明:      1. 执行动态语句,返回语句执行的值。
>>> eval('1+2+3+4')10

  2. 第一个参数为语句字符串,globals参数和locals参数为可选参数,如果提供,globals参数必需是字典,locals参数为mapping对象。

  3. globals参数用来指定代码执行时可以使用的全局变量以及收集代码执行后的全局变量。

>>> g = {
'num':2}>>> eval('num + 2') #num未定义Traceback (most recent call last): File "
", line 1, in
eval('num + 2') File "
", line 1, in
NameError: name 'num' is not defined>>> eval('num + 2',g) #g中有定义num,可执行4

   4. locals参数用来指定代码执行时可以使用的局部变量以及收集代码执行后的局部变量

>>> g = {
'num1':2}>>> l = {
'num2':4}>>> eval('num1+num2',g,l)6

   5. 为了保证代码成功运行,globals参数字典不包含 __builtins__ 这个 key 时,Python会自动添加一个key为 __builtins__ ,value为builtins模块的引用。如果确实要限制代码不使用builtins模块,需要在global添加一个key为__builtins__,value为{}的项即可(很少有人这么干吧)。

>>> g = {}>>> eval('abs(-1)',g)1>>> g = {
'__builtins__':{}}>>> eval('abs(-1)',g) #不能使用内置函数了Traceback (most recent call last): File "
", line 1, in
eval('abs(-1)',g) File "
", line 1, in
NameError: name 'abs' is not defined

   6. 当globals参数不提供是,Python默认使用globals()函数返回的字典去调用。当locals参数不提供时,默认使用globals参数去调用。

>>> num = 1>>> eval('num+2')3>>> globals() #返回字典中含有num的key{
'__doc__': None, 'num': 1, '__package__': None, '__name__': '__main__', '__loader__':
, '__spec__': None, '__builtins__':
}>>> eval('num+2',{}) #locals参数未提供,locals参数=globals参数Traceback (most recent call last): File "
", line 1, in
eval('num+2',{}) File "
", line 1, in
NameError: name 'num' is not defined>>> l = locals() >>> eval('num+2',{},l) #locals参数含有num的key,能求值3>>> locals(){ '__doc__': None, 'l': {...}, 'num': 1, '__package__': None, '__name__': '__main__', '__loader__':
, '__spec__': None, '__builtins__':
}>>>

 

转载于:https://www.cnblogs.com/sesshoumaru/p/5995712.html

你可能感兴趣的文章
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>