详解python eval函数的妙用

作者:zx 时间:2021-12-23 10:56:05 

python eval函数功能:将字符串str当成有效的表达式来求值并返回计算结果。

函数定义:


eval(expression, globals=None, locals=None)

将字符串str当成有效的表达式来求值并返回计算结果。globals和locals参数是可选的,如果提供了globals参数,那么它必须是dictionary类型;如果提供了locals参数,那么它可以是任意的map对象。

python的全局名字空间存储在一个叫globals()的dict对象中;局部名字空间存储在一个叫locals()的dict对象中。我们可以用print (locals())来查看该函数体内的所有变量名和变量值。

Python版本兼容:

  1. Python2.7

  2. Python3.x

eval()主要作用:

1)在编译语言里要动态地产生代码,基本上是不可能的,但动态语言是可以,意味着软件已经部署到服务器上了,但只要作很少的更改,只好直接修改这部分的代码,就可立即实现变化,不用整个软件重新加载。
2)在machin learning里根据用户使用这个软件频率,以及方式,可动态地修改代码,适应用户的变化。

英文解释:

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:

例子:


a=1
g={'a':20}
eval("a+1",g)

结果:
1

例子2, 测试globals, locals


x = 1
y = 1
num1 = eval("x+y")
print (num1)
def g():
x = 2
y = 2
num3 = eval("x+y")
print (num3)  
num2 = eval("x+y",globals())
#num2 = eval("x+y",globals(),locals())
print (num2)

g()

num1的值是2;num3的值也很好理解,是4;num2的值呢?由于提供了globals()参数,那么首先应当找全局的x和y值,也就是都为1,那么显而易见,num2的值也是2。如果注释掉该句,执行下面一句呢?根据第3)点可知,结果为4

实例展示:

可以把list,tuple,dict和string相互转化。


#################################################
字符串转换成列表
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>>type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> type(b)
<type 'list'>
#################################################
字符串转换成字典
>>> a = "{1: 'a', 2: 'b'}"
>>> type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
{1: 'a', 2: 'b'}
>>> type(b)
<type 'dict'>
#################################################
字符串转换成元组
>>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
>>> type(a)
<type 'str'>
>>> b = eval(a)
>>> print b
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
>>> type(b)
<type 'tuple'>

安全问题:

因为eval的特型, 很可能被黑客利用,造成安全问题。

怎么避免安全问题?

1、自行写检查函数;

2、使用ast.literal_eval代替

来源:http://www.pythontab.com/html/2017/hanshu_1109/1181.html

标签:python,eval
0
投稿

猜你喜欢

  • 《色彩解答》系列之三 色彩对比

    2008-02-17 14:40:00
  • 记录集不支持更新。这可能是提供程序的限制,也可能是选定锁定类型的限制

    2010-04-07 22:35:00
  • 详解php处理大并发大流量大存储

    2023-07-21 13:11:02
  • xhtml+css页面制作过程中问题的解决方案

    2008-08-05 18:00:00
  • JS实现pasteHTML兼容ie,firefox,chrome的方法

    2023-09-21 01:55:10
  • python 切片和range()用法说明

    2021-12-12 07:40:52
  • Python可视化Matplotlib折线图plot用法详解

    2021-01-04 03:06:15
  • 浅析python 定时拆分备份 nginx 日志的方法

    2023-08-04 06:03:37
  • PyQt5 PySide2 触摸测试功能的实现代码

    2022-06-23 22:22:25
  • python matplotlib imshow热图坐标替换/映射实例

    2023-05-04 03:46:54
  • javascript实现日期3级联动下拉框选择菜单

    2023-10-19 02:15:34
  • Python中三种时间格式转换的方法

    2023-10-18 13:11:43
  • Python使用pandas对数据进行差分运算的方法

    2021-09-28 06:56:07
  • 2007流行网站导航设计欣赏

    2008-02-18 12:20:00
  • python如何实现单链表的反转

    2023-05-11 12:44:10
  • 原生JS实现左右箭头选择日期实例代码

    2023-08-06 04:55:27
  • SQL学习笔记一SQL基础知识

    2011-09-30 11:24:33
  • Python3爬虫爬取英雄联盟高清桌面壁纸功能示例【基于Scrapy框架】

    2023-03-07 19:19:59
  • Access中的模糊查询

    2007-11-18 14:57:00
  • Symfony模板的快捷变量用法实例

    2023-11-15 04:43:24
  • asp之家 网络编程 m.aspxhome.com