Python built-in function compile ()

Source: Internet
Author: User

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; if it is an eval type, it means that it is a single expression statement that can be used to calculate the corresponding value; Executes in interactive mode, in which case, if it is an expression, it will generally output the result instead of printing to the none output.

Optional parameters flags and Dont_inherit are used to control when compiling the source code, you can view the PEP236 document to understand these parameters, as well as the relevant compilation instructions. If both use the default parameter (that is, both are 0 values), when calling this function compile, the main use of the compiler features indicated in the code to treat; If the flags parameter is set to a value, and Dont_inherit is not set (that is, 0 value), then compiling the code, not only the source of the compilation characteristics of the work, And the features specified in the flags also work, quite a combination of the two; if the parameter dont_inherit is set with a value (that is, a non-0 value), the compile statement only functions when the compiler signature specified by the parameter flags is in effect, that is, the feature specified in the source code is not used.

The compilation feature is set to the parameter by bitmap, you can view the __future__.

The optional parameter optimize is used to indicate the level of optimization used by the compiler, and the default value is-1, which indicates that the optimization level obtained by using the command-line argument-O is the same, if the setting value is 0 (that is, no optimization, __debug__ is set to true), is not optimized, and if the value is set to 1, The Assert statement is deleted, the __debug__ is set to false, and if the setting value is 2, in addition to setting the function with a value of 1, the document description in the code is also removed to achieve optimal optimization results.

When this function compiles code, it returns SYNTAXERROR if 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 the code module will be compiled to prompt the compilation of the source code is not complete error. After Python version 3.2, you are allowed to enter a line break for Windows or Mac; When you use exec mode, you do not need to enter a newline character after each line; After this version, the optimization parameters are added.

1A =" "name = input ("Please enter:")" "2 3t = Compile (A,"',' Single')#compile to interactive mode ' filename ', not read from file, also have to write an empty string4 exec(t)5 Print(name)#name will alarm, no tube6  #compile ()7str ="For i in range (0,10): Print (i)"  8c = Compile (str,"','exec')#compiling to a byte code object9 exec(c)#ExecutionTen Output: One 0 A1 -2 -3 the4 -5 -6 -7 +8 -9 +     ASTR2 ="3*x + 4*y"   atC2 = Compile (str2,"','Eval')#compiling to an expression

? Source--A string or an AST (Abstract Syntax Trees) object:
? FileName-The code file name, which passes some recognizable values if the code is not read from the file.
? Mode-Specifies the kind of compiled code. can be specified as exec, eval, single.
? Flags--variable scope, local namespace, if provided, can be any mapping object:
? Flags and Dont_inherit are used to control when compiling the source code.

Original article: http://blog.csdn.net/caimouse/article/details/41049949

Python built-in function compile ()

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.