英文文档:

eval(expressionglobals=Nonelocals=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 localsdictionaries 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:
>>> x = 1
>>> eval('x+1')
2

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

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

 

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

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

>>> g = {'num':2}

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

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

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

为了保证代码成功运行,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 "<pyshell#9>", line 1, in <module>
eval('abs(-1)',g)
File "<string>", line 1, in <module>
NameError: name 'abs' is not defined

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

>>> num = 1
>>> eval('num+2')
3 >>> globals() #返回字典中含有num的key
{'__doc__': None, 'num': 1, '__package__': None, '__name__': '__main__', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__builtins__': <module 'builtins' (built-in)>} >>> eval('num+2',{}) #locals参数未提供,locals参数=globals参数
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
eval('num+2',{})
File "<string>", line 1, in <module>
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__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__builtins__': <module 'builtins' (built-in)>}
>>>

  

 

转载

最新文章

  1. sql语句注意事项
  2. 获取图片中感兴趣区域的信息(Matlab实现)
  3. lucene索引文件格式
  4. C++ Primer : 第十一章 : 关联容器示例: 一个单词转换的map
  5. 使用wampserver安装Composer的注意事项
  6. Hiddenfield控件
  7. 头文件limits—各个类型的数据的范围
  8. maven多环境部署
  9. UNIX网络编程——产生RST
  10. Android初级教程图片信息
  11. kettle 6.1 按时间循环增量抽取数据
  12. typescript 如何引入jquery
  13. cocos2dx为Sprite添加触摸事件监听器
  14. 【mysql】linux查看mysql配置文件my.conf
  15. jssor/slider图片的问题
  16. linux环境中,read命令的使用?
  17. jetty 热部署
  18. Discuz常见小问题-修改了模块,如何重新移动原有模块的帖子
  19. 08-hibernate注解-总结
  20. Myeclipse10安装git 完美解决-费元星

热门文章

  1. python的flex服务端数据接口开发
  2. 集合综合练习&lt;一&gt;
  3. MYSQL&lt;五&gt;
  4. postman—使用newman来执行postman脚本
  5. 奇怪的 Markdown / LaTeX 笔记
  6. JUnit——assertThat(acture,matcher)
  7. docker安装xxl-job
  8. pyinstaller打包的exe太大?你需要嵌入式python玄学
  9. 配置 Hive On Tez
  10. sqli-labs(19)