如何制作幻灯片模板,清心游,龙田腾子
the eval() takes three parameters:
- expression - this string as parsed and evaluated as a python expression
- globals (optional) - a dictionary
- locals (optional)- a mapping object. dictionary is the standard and commonly used mapping type in python.
将字符串参数当作python代码执行,并返回执行结果。官方文档是这样说的:
the expression argument is parsed and evaluated as a python expression
in [1]: s = 'abc' in [1]: s = 'abc' in [2]: str(s) out[2]: 'abc' in [7]: eval('x') --------------------------------------------------------------------------- nameerror traceback (most recent call last) <ipython-input-7-e5b9369fbf53> in <module>() ----> 1 eval('x') <string> in <module>() nameerror: name 'x' is not defined in [8]: eval('s') out[8]: 'abc'
字符串 s 已经定义过,执行没问题;x未定义,所以报错
这个东西存在的意义?在stackoverflow看到了一个例子:
>>> input('enter a number: ') enter a number: 3 >>> '3' >>> input('enter a number: ') enter a number: 1+1 '1+1' >>> eval(input('enter a number: ')) enter a number: 1+1 2 >>> >>> eval(input('enter a number: ')) enter a number: 3.14 3.14
这样区别就很明显了吧,上面的接收到的是str,下面经过eval处理后,变成了float。
https://www.cnblogs.com/xuuuuuu/p/10127029.html
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论