Python compile() is an inbuilt method that returns a Python code object from the source (normal string, a byte string, or an AST object). The compile() function returns a specified source as the code object, ready to be executed.
Python compile() Function
Python compile() method is used in the Python code is in string form or is an AST object, and you want to change it to a code object. The code object returned by the compile() method can later be called using methods like exec() and eval() which will execute dynamically generated Python code.
Syntax
compile(source, filename, mode, flag, dont_inherit, optimize)
See the following parameters.
Parameter | Description |
---|---|
source | It is Required. The source to compile can be the String, a Bytes object, or an AST object. |
filename | Required. The name of the file that the source comes from. If a source does not come from the file, you can write whatever you like. |
mode | Required. Legal values: eval – if a source is a single expression exec – if a source is a block of statements single – if a source is a unique interactive statement |
flags | Optional. How to compile the source. Default 0 |
dont-inherit | Optional. How to compile the source. Default False |
optimize | Optional. Defines the optimization level of the compiler. Default -1 |
The compile() method returns the Python code object.
See the following code example.
data = compile('print(11)', 'eleven', 'eval') exec(data)
See the following output.
➜ pyt python3 app.py 11 ➜ pyt
The compile() method is used in the Python code is in string form or is the AST object, and you want to modify it to the code object.
Let’s see another programming example.
# app.py codeString = 'x=19\ny=21\nmul=x*y\nprint("mul =", mul)' code = compile(codeString, 'mulstring.py', 'exec') print(code) exec(code)
See the following output.
➜ pyt python3 app.py <code object <module> at 0x103a37660, file "mulstring.py", line 1> mul = 399 ➜ pyt
Here, the source is in standard string form. The filename is mulstring. And, the exec mode later allows the use of exec() method.
The compile() method converts a string to the Python code object. The code object is then executed using the exec() method.
#compile() with eval()
Let’s see the scenario to compile python expression to code and execute it using eval() function.
# app.py el = 11 code = compile('el == 11', '', 'eval') result = eval(code) print(result) code = compile('6 + 5', '', 'eval') result = eval(code) print(result)
See the following output.
➜ pyt python3 app.py True 11 ➜ pyt
#compile() with byte string source
See the following example of using byte string as the source.
# app.py el = 11 bytes_str = bytes('el == 11', 'utf-8') print(type(bytes_str)) code = compile(bytes_str, '', 'eval') result = eval(code) print(result)
See the following output.
➜ pyt python3 app.py <class 'bytes'> True ➜ pyt
#Conclusion
Python compile() function allows us to create the code object from a string, which can be later executed using exec() and eval() functions.
So, Python compile() Example Tutorial is over.