Python

Python Compile() Function

Python is a popular high-level programming language that supports various programming paradigms such as object-oriented, procedural, and functional programming. The language comes with several built-in functions that simplify coding and one such function is the “compile()” function.

This article contains a comprehensive tutorial on the working of the “compile()” function in Python.

What is the “compile()” Function in Python?

The built-in “compile()” function in Python is utilized to compile Python code into “bytecode”.

Syntax

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

 

Parameters

The “compile()” function takes six parameters. The “source”, “filename”, and “mode” parameters are mandatory. The “flags”, “dont_inherit”, and “optimize” parameters are optional. Here is a breakdown of each parameter:

  • source”: This parameter represents the source code, which can either be a string or a file object.
  • filename”: This parameter is a string that represents the file name.
  • mode”: The mode in which the source code is to be compiled. The following modes are supported: “exec”, “eval”, and “single”.
  • flags”: The flags to be utilized when compiling the source code. These flags include “dont_inherit” and “optimize”.

Return

This function gives back the compiled code as a code object. The type of the code object depends on the mode of compilation.

Example 1: Applying the “compile()” Function

Here is an example of utilizing the “compile()” function:

code_str = 'print("Python Guide")'
code = compile(code_str, '<string>', 'exec')
exec(code)

 

In the above code:

  • Define a string that contains the code we want to execute.
  • We then pass this string, along with the “filename” and “mode”, to the “compile()” function, respectively.
  • The resulting code is then executed utilizing the “exec()” function.

Output

The given string has been compiled and shown in the above output.

Example 2: Applying the “compile()” Function by Utilizing the “eval” Mode

Here is another example of the discussed function that utilizes the “eval()” mode:

code_str = '15 + 19'
code = compile(code_str, '<string>', 'eval')
print(eval(code))

 

In the above example code:

  • Define a string that contains the code we want to execute.
  • In the next step, the initialized “string”, “filename” and “mode” are passed to the applied “compile()” function, respectively.
  • The “compile()” function compiles the defined string into a code object.
  • Lastly, the “eval()” function evaluates the code object and retrieves the result.

Output

In the above output, the given string containing the expression “15 + 19” has been compiled into a Python object appropriately.

Conclusion

The built-in “compile()” function in Python converts the code in Python into bytecode. This code can be executed dynamically by the function, which can be used to execute the code at runtime or to check its syntax before execution.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.