Python function-compile ()

Source: Internet
Author: User
Compile (source, filename, mode[, flags[, Dont_inherit]])

英文说明: Compile source into code or AST object. The code object can be evaluated by EXEC statement execution or eval ().

Parameter source: A string or an AST (Abstract Syntax Trees) object.

Parameter filename: The name of the code file, and some recognizable values are passed if the code is not read from the file.

Parameter model: Specifies the kind of compiled code. can be specified as ' exec ', ' eval ', ' single '.

Parameter flag and Dont_inherit: These two parameters are not currently described, optional parameters.


Version: In python2.3, 2.6, 2.7, 3.2 are different, use to attract attention, compatible with Python3


English Description:

Compile the source into a code or AST object. Code objects can be executed by an EXEC statement or evaluated by a call to Eval (). Source can either is a string or an AST object. The Refer to the AST module, documentation for information, is in the work with AST objects.

The filename argument should give the file from which the code is read; Pass some recognizable value if it wasn ' t read from a file ("is commonly used).

The Mode argument specifies what kind of code must is compiled; It can be ' exec ' if source consists of a sequence of statements, ' eval ' if it consists of a single expression, or ' single ' If it consists of a interactive statement (in the latter case, expression statements this evaluate to something OT Her than None would be printed).

The optional arguments flags and Dont_inherit control which future statements (see PEP 236) affect the compilation of sour Ce. If neither is present (or both be zero) The code is compiled with those future statements, is in effect in the code That's calling compile. If The flags argument is given and Dont_inherit are not (or are zero) then the future statements specified by the flags Argu ment is used in addition to those the would be used anyway. If Dont_inherit is a non-zero and the flags argument is it–the future statements in effect around the call to C Ompile is ignored.

Future statements is specified by BITS which can is bitwise ORed together to specify multiple statements. The bitfield required to specify a given feature can is found as the Compiler_flag attribute on the _feature instance in t He __future__ module.

This function raises syntaxerror if the compiled source is invalid, and TypeError if the source contains null bytes.

Note when compiling a string with multi-line code in ' or ' eval ' mode, input must is terminated by at least one new Line character. Facilitate detection of incomplete and complete statements in the code module.

Changed in version 2.3:the flags and Dont_inherit arguments were added.

Changed in version 2.6:support for compiling AST objects.

Changed in version 2.7:allowed use of Windows and Mac newlines. Also input in ' exec ' mode does not has a to end in a newline anymore.


code example:

>>> code = "For I in range (0, ten): Print i" >>> cmpcode = Compile (code, "' EXEC ') >>> exec CMPC ode0123456789>>> str = "3 * 4 + 5" >>> a = compile (str, ', ' eval ') >>> eval (a) 17
  • 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.