Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Find Code Object– compile() Function with Examples

compile() function is used to find the Compile the source into a code or AST object in Python for the given input (string, a byte string, or an AST object).

compile() function is a part of Python Built-in Functions (check list)

Syntax of compile() Function

The syntax of compile() function in Python is:


Parameters of compile() Function in Python

source – Where source is a string, AST object or a byte string. This parameter is required.filename – Where filename is the name of file, where code was read. You can give a name yourself, if the code wasn’t read from a file.This parameter is required.

mode – Where mode is exec (which accepts only a single expression) or eval (a Python code block of statements, class and function etc.) or single (a single interactive statement). This parameter is required.

flags and dont_inherit – Where dont_inherit is a control which future statements affect the compilation of the source. Its default Value is 0. This parameter is optional.

optimize – Where optimize is optimization level of the compiler with default value of -1. This parameter is optional.



compile() function is available and compatible with both Python 2.x and 3.x.


Return Value of compile() Function in Python

compile() function returns a Python code object for the given inputs.


Python compile() Function Example

Total = 100


LearnAndLearn.com © 2018 - All rights reserved