The function compile()
is a built-in function in Python that is used to compile source code into bytecode or code object. In this article, I will tell you how to use the compile
function to convert python source code into bytecode with examples.
1. Python compile() Function Definition.
- The compile() function returns a code object that can be executed by the exec() function, evaluated by the eval() function, or interactively executed by the interact() function.
- The syntax for
compile()
function is:compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)
- source is the source code to be compiled. It can be a string, a code object, or a file object.
- filename is the name of the file from which the code was read. If the code is not read from a file, you can pass an arbitrary string as the filename.
- mode specifies the mode in which the code should be compiled. It can be one of the following three strings:
'exec': If you want to compile a module-level code or a code block that doesn't return a value. 'eval': If you want to compile a single expression that returns a value. 'single': If you want to compile a single interactive statement.
- flags is an optional parameter that can be used to specify additional compiler flags. You can use bitwise OR (|) to combine multiple flags.
- dont_inherit is an optional boolean parameter that specifies whether to inherit the compiler flags from the current scope (False) or to use a fresh set of flags (True).
- optimize is an optional integer parameter that specifies the optimization level of the compiler. The default value is -1, which means that the compiler will use the default optimization level.
2. How To Use Python compile() Function To Convert Python Code To Bytecode Examples.
- Here are some examples that demonstrate how to use the
compile()
function in Python.
2.1 Compile a string and execute it using exec().
- Below is the example source code.
code_str = 'print("Hello, World!")' code_obj = compile(code_str, '<string>', 'exec') exec(code_obj) # Output: Hello, World!
- When you run the above code, it will output the below message on the screen.
Hello, World!
- In this example, we first create a string containing the Python code that we want to compile.
- We then use the
compile()
function to compile the code string into a code object. - Finally, we use the
exec()
function to execute the compiled code.
2.2 Compile a string and evaluate it using eval().
- Below is the example source code.
code_str = '123 * 456' code_obj = compile(code_str, '', 'eval') result = eval(code_obj) print(result)
- When you run the above code, it will output the number 56088 (123 * 456) on the screen.
- In this example, we use the
eval()
function to evaluate the compiled code and print the result.
2.3 Compile a string with additional compiler flags.
- Below is the example source code.
import ast code_str = 'print("Hello, World!")' code_obj = compile(code_str, '<string>', 'exec', flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT) exec(code_obj) # Output: Hello, World!
- In this example, we use the
ast
module to specify an additional compiler flagPyCF_ALLOW_TOP_LEVEL_AWAIT
. - This flag allows the use of top-level
await
statements in the compiled code. - We then compile the code string using the
compile()
function, specifying the additional flag, and execute it using theexec()
function. - You should first import the ast module use the import ast command, otherwise, it will throw the error NameError: name ‘ast’ is not defined.