Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

compile

compile() turns source text, bytes, or an AST into a code object or AST object. The mode selects statement-sequence, expression, or interactive-statement compilation.

How it works

compile() transforms source text, bytes, or an AST into a code object or AST object. Choose the mode according to whether the input is a statement sequence, expression, or interactive statement; the returned code object can then be passed to exec() or eval().

Syntax

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1, *, module=None)

Parameters

NameTypeDescription
sourcestr | bytes | ASTSource text, bytes, or an AST object to compile.
filenamestrSource filename label used for diagnostics.
modestrCompilation mode: "exec", "eval", or "single".
flagsintCompiler flags to activate.
dont_inheritboolWhether compiler options are inherited from the caller.
optimizeintCompiler optimization level.
modulestr | NoneOptional module name.

Returns

A code object or AST object.

Key points

  • compile() converts source into either executable code representation or an AST object.
  • The source input may be text, bytes, or an AST rather than only ordinary source text.
  • mode describes the grammar expected by the compiler and must match the source form.
  • Use "exec" for multiple statements, including assignments, definitions, and control-flow blocks.
  • Use "eval" when compiling exactly one expression whose value can be retrieved.
  • Use "single" for interactive-style input; non-None expression results are printed.
  • module can associate the compiled result with a module name.

Common mistakes

  • Compiling does not execute source code; execute the resulting code object explicitly with exec() or eval().
  • Use a meaningful filename, or a recognizable placeholder such as "<string>", so compiler diagnostics identify the source.

Example: Compile and evaluate an expression

source = "x + 2"
code = compile(source, "<string>", "eval")
result = eval(code)
print(result)

Compiles an expression and evaluates the resulting code object.

When to use it

Precompile expressions

A service receives trusted expression text and needs to evaluate it repeatedly.

Compile generated statements

A scripting tool builds a block of Python statements dynamically.

Model interactive input

An interactive console processes one entered statement at a time.

Interview questions

QWhat does compile() return, and what do its modes mean?

compile() accepts a string, bytes object, or AST and returns a code object or AST object. The mode determines the accepted source shape: "exec" for statements, "eval" for one expression, and "single" for one interactive statement.

Follow-up: When would you use "eval" rather than "exec"? Use "eval" when the input is one expression whose resulting value is needed; use "exec" for a sequence of statements.

QWhat is the purpose of the filename argument?

The filename argument labels the source for diagnostics and should identify the original file when applicable. For generated source, a recognizable value such as "<string>" is suitable.

Follow-up: Why should generated code still receive a filename? Tracebacks and syntax errors can expose that label, making the origin easier to diagnose.

QWhat are the optional compiler-control arguments?

flags and dont_inherit control compiler options and future-feature handling, while optimize selects the compiler optimization level. The module argument optionally supplies the module name.

Follow-up: What should callers do when compiling an AST? Follow the ast module documentation for constructing and working with AST objects.

  • exec
  • eval
  • ast
  • ast.AST

Source: Built-in functions. Checked 2026-10-16.