This function is used to compile a string of source code, the result can be generated bytecode or AST(like syntax tree), bytecode can be executed using function exec () , and AST can use eval () to continue compiling.
The parameter source is the source of a string of strings, or An array of AST objects.
The parameter filename is the file object that reads the string, and if it is not compiled from a file that reads the source code, you can put some strings to identify it.
The parameter mode is used to indicate the type of source code that is represented, and if it is an exec type, it is a sequence statement that can be run, or , in the case of the eval type, Indicates that this is a single expression statement that can be used to calculate the corresponding value , and if it is a single statement, it is executed in interactive mode, in which case, if it is an expression, the result is generally output instead of being printed as None output.
Optional parameter flags and dont_inherit pep236 documents to understand these parameters, as well as instructions for compiling the relevant compilation. If both use the default parameter (that is, both are 0 values), when calling this function compile, the main use of the compiler characteristics indicated in the code to treat, if flags parameter setting has value, and dont_inherit There is no setting (that is, 0 values), then compiling the code is not only a compilation feature of the source works, but also flags dont_inherit set a value (that is, a non-0 value), only the argument when compiling the statement. flags
The compilation feature is set to the parameter by bitmap, you can view the __future__.
Optional parametersoptimizeis used to indicate the level of optimization used by the compiler; The default value is-1, which means using command-line arguments- oThe optimization level obtained in the0(that is, without optimization,__debug__is settrue), is not optimized, if the setting value is1,assertstatement is deleted,__debug__set tofalse; If you set the value to2, in addition to setting a value of1function, it also removes the document description from the code to achieve optimal results.
When this function compiles code, it returns SyntaxErrorif there is a syntax error,and returns the type error TypeError if the code contains some empty bytes .
Note: When compiling with single or eval type, if there are multiple lines of code, there is at least one line break after each line of code, otherwise in Code When the module compiles, it will prompt the compilation of the source code is not complete error. After the Python version of 3.2, enter the line break for Windows or Mac , when using the exec mode, you do not need to enter a line break after each line, and the optimization parameters are added after this version.
Example:
#compile () str = "For I in Range (0,10): Print (i)" C = Compile (str, ', ' exec ') # compiled to byte code object exec (c) # Execute str2 = "3*x + 4 *y "C2 = Compile (str2, ', ' eval ') # compiled as an expression
The output results are as follows:
0
1
2
3
4
5
6
7
8
9
Cai Junsheng qq:9073204 Shenzhen
Python standard library: Built-in function compile (source, filename, mode, flags=0, Dont_inherit=false, Optimize=-1)