1.參數會作為一個 Python 表達式(從技術上說是一個條件列表)被解析並求值 2.去除字元串兩邊的引號 srting 也可以用 3.字元串轉字典 4.傳遞全局變數 5.傳遞本地變數 ...
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'}