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
Let's have a brief look at this
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
If the source contains only a single python expression, you should set the mode to eval
If the source contains multiple python statements, classes and functions and so on, you should set the mode to exec
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
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.
codeString = 'x=10\ny=2\nprint("mul =",x*y)' # this is the code string codeObj = compile(code_str, 'mul.py', '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
x = 1 codeObj = compile('x == 2', '', 'eval') output = eval(codeObj) print(output) # Addition codeObj = compile('x + 10', '', 'eval') output = eval(codeObj) print(output) # Multiplication codeObj = compile('x * 10', '', 'eval') output = eval(codeObj) print(output) # Subtraction codeObj = compile('x - 10', '', 'eval') output = eval(codeObj) print(output) #division codeObj = compile('x / 10', '', 'eval') output = eval(codeObj) print(output)
False 11 10 -9 0.1
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") exec(codeObj)
I like StudyTonight!
compile()with FileReading(reading code from a file)
Here, you have to first create a file compile_with_file_name.py 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 compile_with_file_name.py using the compile method,
f = open('compile_with_file_name.py', 'r') codeString = f.read() f.close() codeObj = compile(code_str, 'compile_with_file_name.py', 'exec') exec(codeObj)
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.
SHARE YOUR THOUGHTS WITH US!