前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python中的eval函数的用法_isnan函数

python中的eval函数的用法_isnan函数

作者头像
全栈程序员站长
发布2022-11-01 09:41:18
9620
发布2022-11-01 09:41:18
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

  eval函数在Python中具有非常重要的地位,熟练的使用eval函数能够为我们的Python编程提供很多的便利之处。在本文中我将详细记录eval函数在Python中的使用方法及它带来便利时带来的一些其他危害,希望您阅读完本文后能够有所收获。欢迎在文章下方留言共同交流学习。

Python eval 函数

一、语法和参数

  在Python中eval()函数的语法格式为eval(expression, globals=None, locals=None),注意后面还有globals参数和locals参数。eval()函数用于执行一个字符串表达式,并且返回该表达式的值。与eval相近的有exec函数,该函数将会在另一篇文章详细讲解。

  • expression:表达式,上面提到eval函数用于执行一个字符串表达式,表达式的内容就放在此处。当表达式涉及到
  • globals该部分必须是字典!必须是字典!必须是字典!否则程序会出错。当定义了globals 参数之后eval函数的作用域会被限定在globals中。
  • locals:该参数掌控局部的命名空间,功能和globals类型,不过当参数冲突时,会执行locals处的参数。

二、expression参数示例

代码语言:javascript
复制
a=10;
print(eval("a+1"))

运行结果为11

【解析】:因为此处没有指定globals和locals,所以直接执行expression部分的内容。该程序的效果等价于a=10 print(a+1)

三、globals参数示例

代码语言:javascript
复制
a=10;
g={ 
   'a':4}
print(eval("a+1",g))

运行结果为5

【解析】:因为现在指定了globals,所以在expression部分的作用域就是globals指定的字典范围内。所以此时外面的a=10被屏蔽,取用字典中的值。

四、locals参数示例

代码语言:javascript
复制
a=10
b=20
c=30
g={ 
   'a':6,'b':8}
t={ 
   'b':100,'c':10}
print(eval('a+b+c',g,t))

运行结果为116 【解析】:根据上面题目的练习我们知道了当有globals和locals时作用的范围域是在globals和locals中,所以a=10,b=20,c=30不会被应用。a和c的值分别去字典g和字典t中的值,当globals和locals中都有相同参数时取locals中的值。所以a=6,b=100,c=10

五、eval函数的危险之处

  eval函数非常的方便,我们可以使用一行代码就实现计算器的功能print(eval(input('请输入')))。但是因为它具有可以将字符串转成表达式执行的特性,所以它也就可以去执行系统命令。这样很容易被别有用心的人用来执行系统命令,删除关键系统文件。

六、eval()函数官方文档

代码语言:javascript
复制
   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')
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.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/203597.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python eval 函数
  • 一、语法和参数
  • 二、expression参数示例
  • 三、globals参数示例
  • 四、locals参数示例
  • 五、eval函数的危险之处
  • 六、eval()函数官方文档
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档