Python compile() function is used to compile the source into code object or AST module object. The returned code object can be executed using exec() or eval() function based on the provided mode to construct the code object.
Python compile()
Python compile() function syntax is:
1 2 3 |
compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) |
Let’s look at the compile() function arguments.
- source: Source to create the code object. This can be string, byte string or AST module object.
- filename: If you are reading code string from a file, you should provide its name here for reference. It’s not used in creating the code object, rather it’s used for making code readable.
- mode: This argument specifies the type of code. Allowed values are
exec
,eval
andsingle
. Use exec if source contains multiple python statements. Use eval if source is a single python expression. Use single if source consists of a single interactive statement. - The optional arguments
flags
anddont_inherit
control which future statements affect the compilation of source. 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(). - The argument
optimize
specifies the optimization level of the compiler.
Python compile() examples
Let’s look at compile() function example with different sources.
Compiling String Source to Code
1 2 3 4 5 6 7 |
# compile() with string source code_str="x=5ny=10nprint("sum =",x+y)" code = compile(code_str, 'sum.py', 'exec') print(type(code)) exec(code) |
Output:
1 2 3 4 |
<span style="color: #008000;"><strong><code> <class 'code'> sum = 15 </code></strong></span> |
Notice that the compile function return type is ‘code’ object. Also, we are using exec() here because source string contains multiple python statements. Notice that code object is being executed by the exec() function and “sum = 15” is getting printed in the console.
Reading Code from a File and Compiling
Let’s say we have my_code.py
file with following content.
1 2 3 4 5 |
x = 10 y = 20 print('Multiplication = ', x * y) |
We can read this file content as a string and compile it to code object and execute it.
1 2 3 4 5 6 7 8 |
# reading code from a file f = open('my_code.py', 'r') code_str = f.read() f.close() code = compile(code_str, 'my_code.py', 'exec') exec(code) |
Output: Multiplication = 200
compile() with eval()
Let’s see an example to compile python expression to code and execute it using eval() function.
1 2 3 4 5 6 7 8 9 10 |
# eval example x = 5 code = compile('x == 5', '', 'eval') result = eval(code) print(result) code = compile('x + 5', '', 'eval') result = eval(code) print(result) |
Output:
1 2 3 4 |
<span style="color: #008000;"><strong><code> True 10 </code></strong></span> |
compile() with byte string source
Let’s look at an example of using byte string as source.
1 2 3 4 5 6 7 |
bytes_str = bytes('x == 5', 'utf-8') print(type(bytes_str)) code = compile(bytes_str, '', 'eval') result = eval(code) print(result) |
Output:
1 2 3 4 |
<span style="color: #008000;"><strong><code> <class 'bytes'> True </code></strong></span> |
compile() with AST Object
Let’s look at an example of using AST object with compile() function.
1 2 3 4 5 6 7 8 |
import ast ast_object = ast.parse("print('Hello world!')") print(type(ast_object)) code = compile(ast_object, filename="", mode="exec") print(type(code)) exec(code) |
Output:
1 2 3 4 5 |
<span style="color: #008000;"><strong><code> <class '_ast.Module'> <class 'code'glt; Hello world! </code></strong></span> |
Summary
Python compile() function allows us to create code object from a string, which can be later executed using exec() and eval() functions. You should take extra care if you are taking user input and compile it to code and execute it because it can have unwanted effects, for example issuing rm -rf /
command.
Reference: Official Documentation