Python provides a description of the built-in functions for calling executable objects, involving exec, Eval, and compile three functions. The EXEC statement is used to execute a python statement stored in a code object, a string, a file, and the eval statement is used to calculate a valid python expression stored in a code object or string, while the compile statement provides a byte-encoded precompilation.
Use of EXEC
Use of Eval
This function is a string used to dynamically execute an expression, or a code object compiled by the compile function. The argument expression is an expression string, or the name of the compiled code object, and the parameter globals is the global namespace, which specifies the scope of the global scope when the expression is executed, such as specifying that some modules are available. If this parameter is defaulted, the current global namespace of the current call to this function is used, and the parameter locals is the local scope namespace, which is used to specify the local namespace to be accessed when an expression is executed. If the global namespace parameter appears, but the default built-in module, the module is automatically copied to the global namespace, meaning that the built-in module can be used regardless of the setting. If two namespaces are used by default, the namespace used when the function is called to find the appropriate variable.
Why use this function? The reason for this function, should be dynamic language and compiled language differences, because in the compilation language to generate code dynamically, is basically impossible, but dynamic language is possible, meaning that the software has been deployed to the server, but as long as a few changes, have to directly modify this part of the code, you can immediately realize the change, Do not reload the entire software. Another, this function can be used in machine learning, for example, according to the user to use the software frequency, as well as the way, can dynamically modify the code to adapt to user changes. Think of here, is not the ability to have vitality, can self-update code, to achieve improved progress, if destructive action, is actually a virus. with exec and Eval, it is important to pay attention to security issues, especially in a network environment, where others may be given the opportunity to execute illegal statements.
Eval is a feature of Python as a dynamic language. Can be used for the following purposes;
(1) Dynamic construction parameters
Opts=struct.uppack (eval ("B '%ds '"% (Opts_len)), ip_header[20:])
(2) Generating a Python object from an external string
For more secure use of Eval, refer to:
http://code.activestate.com/recipes/496746-restricted-safe-/
Http://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval
Use of Compile
Format:compile( str, file, type )
The compile statement is from type types (including ' eval ': Use with eval, ' single ': With the use of exec for a singleton statement, ' EXEC ': With the use of EXEC for multiple statements) to create the statements inside STR into code objects. File is where the code is stored, usually ".
The purpose of the compile statement is to provide a one-time bytecode compilation without having to recompile it in subsequent calls.
It is also important to note that the compile used in compile and regular expressions here are not the same, albeit for the same purpose.
>>> Eval_code = Compile ('1+2',"','Eval')>>>Eval_code<code object <module> at 0142abf0, file"", Line 1>>>>eval (eval_code)3>>> Single_code = Compile ('print "pythoner.com"',"',' Single' )>>>Single_code<code object <module> at 01c68848, file"", Line 1>>>>exec(single_code) pythoner.com>>> Exec_code = Compile ("""For I in range (5): ... print "ITER time:%d"% i""","','exec' )>>>Exec_code<code object <module> at 01c68968, file"", Line 1>>>>exec(Exec_code) ITER time:0iter time:1iter time:2iter time:3iter time:4
Python executable object--exec, eval, compile