首页 > 编程语言 > 详细

python 函数eval

时间:2020-01-02 18:59:26      阅读:78      评论:0      收藏:0      [点我收藏+]

1.参数会作为一个 Python 表达式(从技术上说是一个条件列表)被解析并求值

>>> x = 1
>>> eval(x+1)
2

2.去除字符串两边的引号

>>> a="srting"
>>> print(a)
"srting"
>>> b=eval(a)
>>> print(b)
srting

也可以用

>>> a.strip(")
srting

3.字符串转字典

>>> a= "{‘name‘:‘linux‘,‘age‘:18}"
>>> type(a)
<type str>
>>> b=eval(a)
>>> b
{age: 18, name: linux}
>>> type(b)
<type dict>

4.传递全局变量

>>> a= "{‘name‘:‘linux‘,‘age‘:age}"
>>> b=eval(a,{"age":1822})
>>> b
{age: 1822, name: linux}
>>> type(b)
<type dict>

5.传递本地变量

>>> a= "{‘name‘:‘linux‘,‘age‘:age}"
>>> age=18
>>> b=eval(a,{"age":1822},locals())
>>> b
{age: 18, name: linux}

python 函数eval

原文:https://www.cnblogs.com/hello-wei/p/12134053.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!