Python standard library: built-in function compile (source, filename, mode, flags = 0, dont_inherit = False, optimize =-1)

Source: Internet
Author: User

Python standard library: built-in function compile (source, filename, mode, flags = 0, dont_inherit = False, optimize =-1)

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.

The source parameter is the source code of a string or an array of AST objects.

The filename parameter is a file object that reads strings. If you do not read the source code from the file for compilation, you can put some strings to identify the code.

The mode parameter is used to specify the source code type. If it is exec, it indicates that it is a sequence statement and can be run. If it is eval, it indicates that it is a single expression statement, it can be used to calculate the corresponding value. If it is a single statement, it is executed in interactive mode. In this case, if it is an expression, the result is usually output, instead of printing the output as None.

The optional flags and dont_inherit parameters are used to control the 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), the compilation feature specified in the Code is used to call this function. If the flags parameter is set to a value, dont_inherit is not set (that is, zero value). 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), only the compilation feature value specified by the flags parameter works in the compilation statement, 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 __.

The optional parameter optimize is used to specify the compiler optimization level. The default value is-1, indicating that the optimization level obtained from the command line parameter-O prevails; 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 set to 1, the assert statement is deleted, __debug _ is set to false. If the value is set to 2, in addition to setting the value to 1, the instructions in the code will also be deleted to achieve the best optimization results.

During code compilation, SyntaxError is returned if a syntax error occurs. If the Code contains some NULL bytes, TypeError is returned.

Note: When the single or eval type is used for compilation, if there are multiple lines of code, each line of code contains at least one line break, otherwise, an incomplete source code error is prompted during code module compilation. After Python 3.2, you can enter a line break for Windows or Mac. When the exec mode is used, you do not need to enter a line break after each line. An Optimization Parameter is added after this version.

Example:

# Compile () str = "for I in range (0, 10): print (I)" c = compile (str, '', 'exec ') # compile as a byte code object exec (c) # execute str2 = "3 * x + 4 * y" c2 = compile (str2, '', 'eval') # compile as an expression

The output result is as follows:

0

1

2

3

4

5

6

7

8

9

Cai junsheng QQ: 9073204 Shenzhen

Related Article

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.