Python  Python Library Functions  
   Technology    Programming

Python Compile() Function - Python Library Functions

 NOVEMBER 18, 2019   by vineelpynam

Hello friends, in this tutorial, we are going to have a look at Python's compile() function. The compile function in python converts the source into a code object or AST object. These code object or AST object can be executed by using exec() and eval() functions.

python compile method

Let's have a brief look at this compile() function

Python compile() Function

Syntax for compile() method is:

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

* A Clean look on the compile method parameters,

Source: The source to create a code object. This source can be a normal string, a byte string or an AST object.

Filename: You should pass the filename of the file from which the code is to be read. If you are not reading from a file, you can give a random name as you like.

Mode: There are three modes available. You can use only one per instance that is either exec or eval or single

  1. If the source contains only a single python expression, you should set the mode to eval

  2. If the source contains multiple python statements, classes and functions and so on, you should set the mode to exec

  3. If the source consists of only a single interactive statement, you should set the mode to single

Flags (optional) and dont_inherit (optional): These two parameters control which future statements affect the compilation of the source. The default value is 0 for flags. If both the values are zero(neither is present) then the code is compiled with those future statements that are in effect in the code that is calling the compile() function.

Optimize (optional): It shows the optimization level of the compiler. The default value is -1.

Let's see how the compile() function works with different examples.

Example 1: compile() with exec()

codeString = 'x=10\ny=2\nprint("mul =",x*y)'   # this is the code string 
codeObj = compile(code_str, '', 'exec')   # here the compile method returns the code objects
print(type(codeObj))   # printing the type of the code
exec(codeObj)   # using exec method to execute the compile method


<class 'code'>
mul = 20

Example 2: compile() with eval()

x = 1
codeObj = compile('x == 2', '', 'eval')
output = eval(codeObj)

# Addition
codeObj = compile('x + 10', '', 'eval')
output = eval(codeObj)

# Multiplication
codeObj = compile('x * 10', '', 'eval')
output = eval(codeObj)

# Subtraction
codeObj = compile('x - 10', '', 'eval')
output = eval(codeObj)

codeObj = compile('x / 10', '', 'eval')
output = eval(codeObj)



Example 3: compile() with AST(Abstract Syntax Trees) Objects

import ast   # to use AST, first you have to import it.
astObj = ast.parse("print('I like StudyTonight!')")
codeObj = compile(ast_object, filename="studytonight", mode="exec")



I like StudyTonight!

Example 4: compile() with FileReading(reading code from a file)

Here, you have to first create a file and then place the below code in it and save it.

x = 5
y = 10
print('ADD = ', x + y)
print('SUB = ', x - y)
print('MUL = ', x * y)
print('DIV = ', x / y)

Reading the code from using the compile method,

f = open('', 'r')
codeString =
codeObj = compile(code_str, '', 'exec')


ADD =  15
SUB =  -5
MUL =  50
DIV =  0.5


Pheww! This is it. I hope that you enjoyed the post and learned about the compile() python library function. If you feel that this post is useful, please share it with your friends and colleagues.

Thanks for reading it till the end.