exec obj
Function:
EXEC executes python statements stored in a string or file, and can execute more complex Python code than eval,exec. obj is the expression to execute.
The exec return value is always None.
Release Notes:* * It is necessary to note that Exec is not a function in Python2, but a built-in statement (statement), but there is a execfile () function in Python 2.
It can be understood that Python 3 consolidates the functionality of the EXEC statement and execfile () functions into a new EXEC () function.
Instance:
1>>>exec 'print "Hello World"'2 Hello World3 #single-line statement string4>>>exec "print ' runoob.com '"5 runoob.com6 7 #multi-line statement string8>>>exec """For I in range (5):9 ... print "ITER time:%d"% iTen ... """ One ITER time:0 AITER time:1 -ITER Time:2 -ITER Time:3 theITER Time:4
Example 2:
1x = 102Expr ="""3 z =4 sum = x + y + z5 print (sum)6 """7 deffunc ():8y = 209 exec(expr)Ten exec(Expr, {'x': 1,'y': 2}) One exec(Expr, {'x': 1,'y': 2}, {'y': 3,'Z': 4}) A -Func ()
Results:
1 2 3 34
Python exec built-in expression--exec ()