Python built-in function-compile

Source: Internet
Author: User
This function is used to compile the source code of a string. The result can generate bytecode or AST (image pulling syntax tree). The bytecode can be executed using the exec () function, and AST can use eval () to continue compilation. 2. the source parameter is the source code of a string or an array of AST objects.

Python built-in function-compile

Compilecompile (source, filename, mode [, flags [, dont_inherit, [optimize])
1. this function is used to compile the source code of a string. The result can generate bytecode or AST (image syntax tree ),
Bytecode can be executed using the exec () function, while AST can use eval () to continue compilation.
2. the source parameter is the source code of a string or an array of AST objects.
3. the filename parameter is the object for reading strings,
If the source code is not read from the file for compilation, you can put some strings to identify the code.
4. the mode parameter is used to specify the source code type;
If the exec type is used, it indicates that this is a sequence statement and can be run;
If it is of the eval type, it indicates that this is a single expression statement and can be used to calculate the corresponding value;
If it is of the single type, it indicates that this is a single statement and is executed in interactive mode,
In this case, if it is an expression, the result is usually output, instead of printing as None.
5. the optional flags and dont_inherit parameters are used to control the flag during source code compilation,
You can view PEP236 documents to learn about these parameters and related compilation instructions.
If both use the default parameter (that is, both are zero values ),
When calling this function for compilation, we mainly use the compilation features specified in the code;
If the flags parameter is set with a value but the dont_inherit parameter is not set (that is, the value is zero ),
When compiling the code, not only does the compilation feature of the source code take effect, but also the features specified by flags take effect, equivalent to the union of the two;
If the dont_inherit parameter has a value (that is, a non-zero value ),
When compiling a statement, only the compilation feature value specified by the flags parameter works, that is, the feature specified in the source code is not used.
The compilation feature is set to parameters in bitmap mode. you can view _ Ure __.
6. the optional parameter optimize is used to specify the compiler optimization level;
The default value is-1, indicating that the optimization level obtained in-O is used;
If the value is set to 0 (that is, optimization is not required, __debug _ is set to true), there is no optimization;
If the value is 1, The assert statement is deleted, __debug _ is set to false;
If the value is set to 2, in addition to the function of setting the value to 1, the instructions in the code are also deleted to achieve the best optimization results.
7. SyntaxError is returned if a syntax error occurs during code compilation of this function;
If the code contains some null bytes, type error TypeError is returned.
Note:
1. when the single or eval type is used for compilation,
If there are multiple lines of code, each line of code should be followed by at least one line break \. Otherwise, the code module will prompt that the compiled source code is incomplete.
After Python 3.2, you can enter a line break for Windows or Mac;
2. when the exec mode is adopted, line breaks do not need to be entered after each line. The optimization parameters are added after this version.

In [16]: str = "for I in range (10): print (I)" In [17]: c = compile (str, '', 'exec ') # compile as a byte code object In [18]: exec (c) 0123456789In [19]: str = "3 * x + 4 * y" In [20]: c = compile (str, '', 'eval') # compile as the expression In [23]: x, y = 3, 4In [24]: eval (c) out [24]: 25

The above is the content of compile, a Python built-in function. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.