×

Python Compiler

What is Compiler?

The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using a high-level language (C, C++, Java and a lot more) or any source language.

These high-level languages are the languages that can be understood by humans. However, these languages cannot be understood by the computer. Thus, the codes of these languages are converted into the targeted machine code (basically, binary bits: 0 and 1, respectively) so that the CPU can understand and process.

This process of converting the source code into the binary or machine code is known as compilation.

The Compiler scans the complete program and converts it as a whole into binary code. It generally takes pretty much time to analyze the source code. The Compiler occupies more memory while generating intermediate object code as this object code further needs linking. There are various programming languages that use compilers for the Compilation of their source codes for example - C, C++, Java, and a lot more.

The Compiler’s task is usually divided into different phases. These phases involve:

  • Lexical Analysis
  • Syntax Analysis
  • Sematic Analysis
  • Intermediate code generator
  • Code optimizer
  • Code generator

These phases support converting the source code by breaking it down into tokens, producing parse tress and optimizing the source code.

Understanding the process of Compilation and Linking in Python

We know that Python’s source codes are saved as the .py extension files. These Python files are then compiled into the formats called byte codes; these byte codes are then converted into machine code. The Compilation is referred to as a translation step. Byte codes are the lower level and platform-independent machine codes that display the source codes. Once the compilation is done, the codes are saved in the .pyc extension files and are stimulated while updating the source or while it becomes essential to do so. This process is referred to as a Compilation.

Linking in Python

Linking is the process and most preferably the last phase where every function is linked with its definition because the linker knows where all the functions are implemented.

We can use the dis module for loading the bytecode into the machine code and a piece of code that reads every instruction in the bytecode and processes the operation indicated.

Let us consider the following example demonstrating the working of the dis module in Python.

Example:

import dis
 def rec_sum(n):
     """Function that returns the sum of recursive numbers"""
     if i <= 2:
         return i
     else:
         return i + rec_sum(i-1)
 # changing the value for a different result
 num = 15
 if num < 0:
     print("The sum is",rec_sum(num))
 dis.dis(rec_sum) 

Output:

 4           0 LOAD_GLOBAL              0 (i)
               2 LOAD_CONST               1 (2)
               4 COMPARE_OP               1 (<=)
               6 POP_JUMP_IF_FALSE       12
   5           8 LOAD_GLOBAL              0 (i)
              10 RETURN_VALUE
   7     >>   12 LOAD_GLOBAL              0 (i)
              14 LOAD_GLOBAL              1 (rec_sum)
              16 LOAD_GLOBAL              0 (i)
              18 LOAD_CONST               2 (1)
              20 BINARY_SUBTRACT
              22 CALL_FUNCTION            1
              24 BINARY_ADD
              26 RETURN_VALUE
              28 LOAD_CONST               3 (None)
              30 RETURN_VALUE 

In the above example, we have imported the dis module. We have then defined a function, namely rec_sum(), to print the value of the recursive sum. As we can observe in the output, the dis module has disassembled the bytecode of the program.

Understanding the compile() method in Python

Python provides the compile() method that helps return a Python code object from the source (a byte string, a normal string or an AST object).

Syntax

The compile() method has the following syntax:

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

The compile() method helps in converting the string form or an AST object of the Python code into the code object.

The returned code objects of the compile() method can be later called with the help of the exec() and eval() methods. These methods will execute the dynamically produced Python code.

Parameters of the Python’s compiler() method

Now let’s have a brief look at the parameters of Python’s compile() method:

  1. source: The source can be considered as a normal string, an AST object or a byte string.
  2. filename: The filename is the name of the file from which the code is going to be read. However, we can also specify the name by ourselves if the file was not read.
  3. mode: The mode can either be exec or eval or single.
    1. eval: eval accepts only a single expression
    1. exec: exec accepts a code block containing Python statements, classes and functions and a lot more.
    1. single: single accepts only single interactive statements
  4. flags(optional) and dont_inherit(optional): These are the optional arguments available for controlling the effect of future statements over the compilation of the source. By default, the value is assigned to zero (0).
  5. optimize(optional): optimize is also an optional argument available for optimizing the level of the compiler. By default, the value is assigned to -1.

The return value from Python’s compile() method

The Python’s compile() method returns a code object for Python.

Let’s understand the working of the compile() method in Python with the help of an example shown below:

Example:

 code_in_Str = 'a = 7\nb = 8\ns = a + b\nprint("Total =", s)'
 codeObject = compile( code_in_Str, 'comp.py', 'exec' )
 print(type(codeObject))
 exec(codeObject) 

Output:

<class ‘code’>
Total: 15

In the above example, we have created a string called code_in_Str that acts as the source for the compile() method. We have saved the python program file as comp.py.

Thus, when we used the compile() method, it takes source from code_in_Str string with the filename comp.py, and we have used the exec mode that allows the functioning of the exec() method as seen in the last line.

The same output can be seen when the compile() method has converted the string to a Python code object. And the execution of the code object is done using the exec() method. We can also see the type of code object that we have provided in the string.


Related Topics

Applications of Python

Top 10 Applications of Python in 2020- 2021 Python language is famous for its general-purpose nature, which enables developers to use it in every field of development. Python can be found...

6 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

How to build an Auto Clicker using Python?

What is an Auto Clicker? An Auto Clicker is software that is used for automating the click of a mouse on any particular element on the computer screen and controlling the...

8 minutes read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Tuple in Python

Tuples Tuples are the same as the list but as we know that lists are mutable means we can change the data from it. In the tuple we cannot change the...

6 minutes read.

App Config Python

An XML file called App. Config serves as the document for any programme. In other terms, you can modify any configuration inside of it without going to edit the code...

7 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 minutes read.

Python Breakpoint

Introduction In Python 3.7, a brand-new created function called breakpoint() was added. Due to the close relationship between both the executable and the code of a debugging component, debugging Python programming...

4 minutes read.

Python input() function

Python input() function The input() function in Python reads a line from input and converts it to a string (stripping a trailing newline). Syntax input([prompt]) Parameter prompt: This function represents a string, depicting a default...

1 minute read.

Iterate a Dictionary in Python – Part 2

In this tutorial, we will learn above various methods used to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to...

3 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Python TCP Server

The TCP server library is also known as the socket library. Socket programming is the method of connecting two nodes through a network to form communication between two nodes. In...

3 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.