Python的eval()函数怎么用

发布时间:2022-01-19 16:48:34 作者:iii
来源:亿速云 阅读:240

这篇文章主要介绍“Python的eval()函数怎么用”,在日常操作中,相信很多人在Python的eval()函数怎么用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python的eval()函数怎么用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、语法和参数

在Python中evel()函数的语法格式为eval(expression, globals=None, locals=None),注意后面还有globals参数和locals参数。eval()函数用于执行一个字符串表达式,并且返回该表达式的值。

二、expression参数示例

a=10;
print(eval("a+1"))

运行结果为11

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

三、globals参数示例

a=10;
g={'a':4}
print(eval("a+1",g))

运行结果为5

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

四、locals参数示例

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中都有参数b时取locals中的值。所以a=6,b=100,c=10

五、eval函数的危险之处

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

六、eval()函数官方文档

   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.

附eval()函数常见作用有

1、计算字符串中有效的表达式,并返回结果

>>> eval('pow(2,2)')
4
>>> eval('2 + 2')
4
>>> eval("n + 4")
85

2、将字符串转成相应的对象(如list、tuple、dict和string之间的转换)

>>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>> b = eval(a)
>>> b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> a = "{1:'xx',2:'yy'}"
>>> c = eval(a)
>>> c
{1: 'xx', 2: 'yy'}
>>> a = "(1,2,3,4)"
>>> d = eval(a)
>>> d
(1, 2, 3, 4)

3、将利用反引号转换的字符串再反转回对象

>>> list1 = [1,2,3,4,5]
>>> `list1`
'[1, 2, 3, 4, 5]'
>>> type(`list1`)
<type 'str'>
>>> type(eval(`list1`))
<type 'list'>
>>> a = eval(`list1`)
>>> a
[1, 2, 3, 4, 5]

到此,关于“Python的eval()函数怎么用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. python和Visual Studio Code是什么
  2. python浅拷贝和深拷贝的区别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python eval()

上一篇:HTML该怎样理解

下一篇:小程序中如何使用wx.writeBLECharacteristicValue

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》