What is Python compile() function?

Python Compile() Function (1)

Hey, folks! In this article, we will be focusing on Python compile() function.


Understanding the Working of Python compile() function

Let me take you all back to the system/OS programming from where we deal with the concept of macros and functions. Macros are basically some pre-defined code blocks that get executed with the current source code upon a function call. That is, the entire functional block gets executed in one shot within any working program code.

On the similar lines, Python compile() function helps us define a piece of code within the function. Further, it generates a code block object, that can be used to execute the defined code at any point within the program.

The input-source-code defined within the compile() function can be easily executed in any program through the code object returned from the function.

Thus, Python compile() function helps attain reusability and proves out to be more reliable.


Syntax of Python compile() function

Python compile() function accepts the source code as argument and returns the code object which is readily available for execution.

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

Now let us understand the parameter list of Python compile() function in detail.


Parameters of compile() function

  • source(Required): It is the source code or the byte string to be compiled.
  • filename(Required): It is the name of the file which contains the ‘source’. If not present, we can enter a name for the source code.
  • mode(Required): It is the mode within which the source would be compiled. These three modes are as follows:
  1. eval: This mode is used when the source is a single expression to compile.
  2. single: Used when the source contains a single interactive statement.
  3. exec: This mode is used when the source contains a block of statements to execute.

Optional Parameters:

  • flags: Default value is 0.
  • dont_inherit: Decides the flow of executions. The default value is False.
  • Optimize: Default value is -1.

Examples of Python compile() function

In the below example, we have passed a single variable ‘var = 10’ as a source code to the compile() function. Further, we have used ‘single mode‘ to compile and execute the source passed to the argument list.

var = 10
compile_obj = compile('var', 'Value', 'single')
exec(compile_obj)

Using compile() function, the code object associated to the source code passed gets created.

Then, Python exec() function is used to dynamically compile the code object.

Output:

10

Now, we have passed a single expression for execution to the compile() function and thus, ‘eval mode‘ is being used here.

action = 'print(100)'
compile_obj = compile(action, 'Display', 'eval')

exec(compile_obj)

Output:

100

As seen below, we have passed a string of source code to the compile() function and have used ‘exec mode‘ to execute the code blocks through the code object created using exec() function.

action = 'x = 15\ny =10\nproduct=x*y\nprint("Product of x and y =",product)'
compile_obj = compile(action, 'Product', 'exec')

exec(compile_obj)

Output:

Product of x and y = 150

Conclusion

By this, we have come to the end of this topic. Please feel free to comment below in case you come across any doubt.

For more such posts related to Python Programming, please do visit Python Tutorials AskPython.


References