[Switch] eval () function usage, eval function usage
Eval
Function: Evaluate a string 'str' as a valid expression and return the calculation result.
Syntax: eval (source [, globals [, locals])-> value
Parameters:
Source: Code object returned by a Python expression or function compile ()
Globals: Optional. Must be a dictionary
Locals: Optional. Any map object
Instance display:
1. list, tuple, dict, and string can be converted to each other. 2 ####################################### ########## 3 convert a string to a list 4 >>> a = "[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0] "5 >>> type () 6 <type 'str'> 7 >>> B = eval (a) 8 >>> print B 9 [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0] 10 >>> type (B) 11 <type 'LIST'> 12 ################################ ################ 13. convert a string to a dictionary 14 >>> a = "{1: 'A', 2: 'B'} "15 >>> type (a) 16 <type 'str'> 17 >>> B = eval () 18 >>> print b19 {1: 'A', 2: 'B'} 20 >>> type (B) 21 <type 'dict '> 22 ################################ ################ 23 convert a string to a metagroup 24 >>> a = "([1, 2], [3, 4], [5, 6], [7, 8], (9, 0) "25 >>> type () 26 <type 'str'> 27 >>> B = eval (a) 28 >>> print b29 ([1, 2], [3, 4], [5, 6], [7, 8], (9, 0) 30 >>> type (B) 31 <type 'tuple'>
Reference: http://www.cnblogs.com/liu-shuai/p/6098246.html