Author's blog address: https://www.cnblogs.com/liu-shuai/
Eval
Function: evaluates the string str as a valid expression and returns the result of the calculation.
Syntax: eval (source[, globals[, locals]), value
Parameters:
Source: The code object returned by a Python expression or function compile ()
Globals: Optional. It must be dictionary.
Locals: Optional. Arbitrary map Object
Examples show:
1 can convert list,tuple,dict and string to each other. 2 ################################################# 3 string converted to List 4 >>>a = "[[], [3,4], [5,6], [7,8], [9,0]]" 5 & Gt;>>type (a) 6 <type ' str ' > 7 >>> b = eval (a) 8 >>> print B 9 [[1, 2], [3, 4], [5, 6], [7, 8 ], [9, 0]]10 >>> type (b) <type ' list ' >12 ################################################ #13 String converted to dictionary >>> a = "{1: ' A ', 2: ' B '}" (a) <type ' str ' >17 >>> B = eval (a) &G t;>> print B19 {1: ' A ', 2: ' B '}20 >>> type (b) <type ' dict ' >22 #################################### ############ #23 string Conversion Narimoto Group >>> a = "([up], [3,4], [5,6], [7,8], (9,0))" >>> type (a) <type ' str ' & gt;27 >>> B = eval (a) >>> print b29 ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))->>> type (b) 31 <type ' tuple ' >
Python eval function Magical