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'}