Eval function
function of Functions
Evaluates the string str as a valid expression and returns the result of the calculation. The Python code it executes can only be a single operation expression (which does not support arbitrary assignment operations), not complex code logic.
Second, the definition of function
eval (expression, Globals=none, Locals=none)
Parameter description:
Expression: A required parameter, either a string or an arbitrary instance of the code object (which can be created through the compile function). If it is a string, it is parsed and interpreted as a python expression.
Globals: An optional parameter that represents the global namespace (which holds the global variable) and, if provided, a Dictionary object.
Locals: An optional parameter that represents the current local namespace (which holds local variables) and, if provided, can be any mapping object. If the parameter is ignored, then it will take the same value as globals.
If both Globals and locals are ignored, they will take the global namespace and local namespace of the eval () function called environment.
return value:
if Expression is a codecompile when the song body" "> object mode parameter is ' EXEC ' So Eval () none
Otherwise, if expression is an output statement, such as print (), eval () Returns the result of None
Otherwise, the result of expression expressions is the return value of the eval () function
Three, to give a few chestnuts
(1) Evaluates a valid expression in a string and returns the result
[CPP]View PlainCopy
- >>> eval (' pow (2,2) ')
- 4
- >>> eval (' 2 + 2 ')
- 4
(2) converts a string into a corresponding object (such as A conversion between list, tuple, dict, and string)
[Python]View PlainCopy
- >>> a = "[[+], [3,4], [5,6], [7,8], [9,0]]"
- >>> B = eval (a)
- >>> b
- [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
- >>> A = "{1: ' xx ', 2: ' yy '}"
- >>> C = eval (a)
- >>> C
- {1: ' xx ', 2: ' yy '}
- >>> a = "(1,2,3,4)"
- >>> d = eval (a)
- >>> D
- (1, 2, 3, 4)
(3) Changing the value of a variable
[CPP]View PlainCopy
- x = 10
- def func ():
- y = 20
- A = eval (' x+y ')
- Print "A=", a
- b = eval (' X+y ', {' x ': 1, ' Y ': 2})
- Print "b=", b
- c = eval (' X+y ', {' x ': 1, ' Y ': 2}, {' y ': 3, ' Z ': 4})
- Print "c=", C
- Func ()
Output:
A= 30
B= 3
C= 4
EXEC function
function of Functions
Dynamic execution of Python code that can execute complex python code
Second, function definition
EXEC (object[, globals[, locals]]
Parameter description:
Object: A required parameter that represents the python code that needs to be specified , which must be a string or a code object. If object is a string, the string is parsed into a set of python statements before being executed. If object is a code object, then it is simply executed
Globals: Optional parameter, same as eval function
Locals: Optional parameter, same as eval function
return value:
The return value of the EXEC function is always None
Three, chestnuts
(1) example 1
Because the Exec return value is None
So a = EXEC (' x+y '), this code will error.
Instead of exec (' x+y '), it can be executed normally
and d = eval (' Print (x, y) '), this code will error
Change to D = Exec (' Print (x, y) ')for normal execution
(2) Example 2
[Python]View PlainCopy
- x = Ten
- Expr = "" "
- z = 30
- sum = x+y+z
- Print (sum)
- """
- def func ():
- y =
- exec (expr)
- exec (expr, {' x ': 1, ' y ': 2})
- exec (expr, {' x ': 1, ' y ': 2}, {' y ': 3, ' z ': 4}) /c1>
- Func ()
Output:
60
33
34
For the output of the last result, why Z is not 4, comb the execution process as follows:
[Python]View PlainCopy
- x=1
- y=2
- def func ():
- y=3
- z=4
- z=
- Sum=x+y+z
- print (sum)
- Func ()
The eval () function differs from the EXEC () function:
The eval () function evaluates only the values of a single expression, and the exec () function can run code snippets dynamically
The eval () function can have a return value, and the exec () function return value is always None
Original: 75049938
Python's eval, exec summary of function usage