Eval () function
eval () The function explained in the official document is to convert a string object into a valid expression to participate in the evaluation operation to return the calculated result
Syntactically: The call is: eval (expression,globals=none, Locals=none) Returns the result of the calculation
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:
1you can convert list,tuple,dict and string to each other. 2#################################################3string conversion to list4 >>>a ="[ [up], [3,4], [5,6], [7,8], [9,0]]"5 >>>type (a)6 <type'Str'> 7 >>> b =Eval (a)8 >>>Printb9 [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]Ten >>>type (b)<type'List'>12#################################################13convert strings into dictionaries>>> A ="{1: ' A ', 2: ' B '}">>>type (a)<type'Str'>17 >>> B =Eval (a)>>>Printb19 {1:'a', 2:'b'}>>>type (b)<type'Dict'>22#################################################23string Conversion Narimoto Group>>> A ="([up], [3,4], [5,6], [7,8], (9,0))">>>type (a)<type'Str'>27 >>> B =Eval (a)>>>Printb29 ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))>>>type (b)<type'tuple'>
Reference:
Http://www.cnblogs.com/dadadechengzi/p/6149930.html
Python Learning notes-eval function