Python standard library: built-in function exec (object [, globals [, locals]), pythonglobals
This function is used to execute a statement or function. The parameter object is the name of a string statement or compiled statement. The globals parameter is a global namespace used to specify the global namespace that can be accessed during statement execution. The locals parameter is a local namespace used to specify the namespace of the local scope that can be accessed during statement execution. Note that this function does not return any value, regardless of whether the function or statement has any return statement, such as the return or yield statement.
If the globals and locals parameters are ignored, the namespace used for the call will be used. Both parameters require a dictionary to describe the namespace.
I have learned compile, eval, and other functions before. What are the differences between them? The differences between them are as follows:
The compile function only compiles string code without any execution. However, it can compile expressions or statements.
The eval function only executes the expression string code without executing the statement code.
X = eval ('% d + 6' % x)
The exec function only executes the statement code without executing the expression code, because it does not return any value.
Exec ('If True: print (6 )')
Example:
# Exec () exec ('If True: print (100) ') exec (''' x = 200if x> 100: print (x + 200 )''')
The output is as follows:
100
400
Cai junsheng QQ: 9073204 Shenzhen