没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
python3中中eval函数用法使用简介函数用法使用简介
主要介绍了python3中eval函数用法使用简介,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有
一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
python中eval函数的用法十分的灵活,这里主要介绍一下它的原理和一些使用的场合。
下面是从python的官方文档中的解释:
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:
>>> x = 1
>>> eval('x+1')
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(expression, globals=None, locals=None)
参数:
expression:这个参数是一个字符串,python会使用globals字典和locals字典作为全局和局部的命名空间,将expression当做
一个python表达式(从技术上讲,是一个条件列表)进行解析和计算。
globals:这个参数管控的是一个全局的命名空间,也就是我们在计算表达式的时候可以使用全局的命名空间中的函数,如果这
个参数被提供了,并且没有提供自定义的__builtins__,那么会将当前环境中的__builtins__拷贝到自己提供的globals里,然后
才会进行计算。关于__builtins__,它是python的内建模块,也就是python自带的模块,不需要我们import就可以使用的,例
如我们平时使用的int、str、abs等都在这个模块中。如果globals没有被提供,则使用python的全局命名空间。
locals:这个参数管控的是一个局部的命名空间,和globals类似,不过当它和globals中有重复的部分时,locals里的定义会覆
盖掉globals中的,也就是当globals和locals中有冲突的部分时,locals说了算,它有决定权,以它的为准。如果locals没有被
提供的话,则默认为globals。
eval函数也可以被用来执行任意的代码对象(如那些由compile()创建的对象)。在这种情况下,expression参数是一个代码对
象而不是一个字符串。如果代码对象已经被‘exec‘编译为模式参数,eavl()的返回值是None。
下面举一些例子进行讲解:下面举一些例子进行讲解:
三个参数的使用:
1、在前两个参数省略的情况下,eval在当前的作用域执行:
a=10;
print(eval("a+1"))
执行结果为:11
在这种情况下,后两个参数省略了,所以eval中的a是前面的10。对于eval,它会将第一个expression字符串参数的引号去
掉,然后对引号中的式子进行解析和计算。
2、在globals指定的情况下:
a=10;
g={'a':4}
资源评论
weixin_38687277
- 粉丝: 10
- 资源: 949
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 黑苹果OC引导-0.9.1
- Redis 服务等过期策略和内存淘汰策略解析
- debian配置FTP服务
- 基于Matlab和CPLEX的2变量机组组合调度程序(注释完全,可直接运行)(文档加Matlab源码)
- 基于TMS320F2812设计复合频率信号频率计AD09硬件(原理图+PCB )+CCS软件源码+详细设计文档资料.zip
- MultivariateAnalysis(目标规划、多元分析与插值的相关例子)(注释完全,可直接运行)(文档加Matlab源码)
- 黑苹果OC引导-0.9.2
- 数据库实验-王珊.doc
- unity读取excel工具 使用3.5即可
- Matplotlib 是一个 Python 的绘图库 Matplotlib 绘图指南与功能介绍.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功