1.参数会做为一个 Python 表达式(从技术上说是一个条件列表)被解析并求值python
>>> x = 1 >>> eval('x+1') 2
2.去除字符串两边的引号linux
>>> a='"srting"' >>> print(a) "srting" >>> b=eval(a) >>> print(b)
srting
也能够用code
>>> a.strip('"') 'srting'
3.字符串转字典ip
>>> 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.传递本地变量class
>>> a= "{'name':'linux','age':age}" >>> age=18 >>> b=eval(a,{"age":1822},locals()) >>> b {'age': 18, 'name': 'linux'}