Python compile function:

The compile() is a built-in function in Python that takes source code as input and returns a ready to execute code or AST object. Moreover, this function raises SyntaxError if the compiled source is invalid and ValueError if the source contains null bytes.

Signature

The signature for the compile() function is as shown below. It takes six parameters and returns the code object.

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

Parameters and return type

  • Here, The compile() function takes three mandatory and three optional parameters.
    1. source – This can either be a normal string, a byte string, or an AST object.
    2. filename – This specifies the file’s name from which the code is read. We can also pass a string if it contains some recognizable code.
    3. mode – The mode argument specifies what kind of code must be compiled. It can be ‘exec’ if the source consists of a sequence of statements. However, it will be ‘eval’, if it consists of a single expression. It can be ‘single’ if it consists of a single interactive statement.
    4. flags – This is an optional parameter that tells how to compile the source. The default value is 0.
    5. dont_inherit – This is an optional parameter that takes care of all the future statements that affect the compilation of the source.
    6. optimize – This is an optional parameter that specifies the compiler’s optimisation level. The default value is -1.
  • However, it returns the ready to execute code.

Python compile() Examples:

Example 1: In this example, We will demonstrate compiling with a single statement. Firstly, we will take str as a string containing a single statement to execute. After that, we will print the object returned by the compile and the compiled object’s execution.

#Initializing
str= 'print("Python Programming")'
#Using compile function
x = compile(str, '', 'single')
#printing the object
print(x)
#Executing the object
exec(x)

Output

<code object  at 0x7f3cba136500, file "", line 1>
Python Programming

Example 2: In this example, We will demonstrate the working of compile with exec mode. Firstly, let us take a file test.py containing the below-coded code.

s="Python Programming"
print(s)

Here, we will read the file content as a string and then compile it to a code object that the exec function will execute.

# reading code from a file
f = open('test.py', 'r')
x = f.read()
f.close()
code = compile(x, 'test.py', 'exec')
exec(code)

Output

Python Programming

Example 3: In this example, we will demonstrate the working of compile with eval mode. As we are initializing variable a with a value 10, condition a to be equal to 50 goes wrong. As a result, it prints False.

# Initializing
a = 10
# Note eval is used for statement
obj = compile('a == 50', '', 'eval')
print(eval(obj))	

Output

False

Conclusion

The compile() function returns a ready to execute code or AST object. So, the exec function executes this.

References

Happy Learning 🙂

Leave A Comment