compile(string, filename, kind[, flags[, dont_inherit]])
Compiles the string into a code object. Code objects can be executed by
an exec statement or evaluated by a call to eval. The filename
argument should give the file from which the code was read; pass some
recognizable value if it wasn’t read from a file (
is commonly used). The kind argument specifies what kind of code must
be compiled; it can be
'exec' if string consists of a sequence of
'eval' if it consists of a single expression, or
'single' if it consists of a single interactive statement (in the
latter case, expression statements that evaluate to something else
than None will be printed).
When compiling multi-line statements, two caveats apply: line endings
must be represented by a single newline character (
'\n'), and the
input must be terminated by at least one newline character. If line
endings are represented by
'\r\n', use the string replace() method
to change them into
The optional arguments flags and dont_inherit (which are new in Python 2.2) control which future statements (see PEP 236) affect the compilation of string. If neither is present (or both are zero) the code is compiled with those future statements that are in effect in the code that is calling compile. If the flags argument is given and dont_inherit is not (or is zero) then the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it — the future statements in effect around the call to compile are ignored.
Future statements are specified by bits which can be bitwise or-ed together to specify multiple statements. The bitfield required to specify a given feature can be found as the ‘compilerflag’ attribute on the ‘Feature’ instance in the __future__ module.