What is __future__ module in the Python

In this post, we are going to learn about the module in the Python which is named “__future__”.

This is a very interesting module from Python from which many compatibility issues can be solved! Unlike other modules that have many tools to enhance or to minimize the work of the coding, the __future__ module is another part of the page.py

What is the need of the __future__ module?

As the new version will be updated there are chances of inclusion of the new functions for the betterment and enhancement of the code.

This will create ambiguity for the users which will lead to increasing the chances of the error. To avoid this chaos of the __future__ module will be the most useful option here.

What exactly is the __future__ module is?

As we have seen above there are certain functions in the Python which are not compatible with the older version and this function can be utilized with the help of the __future__ module.

The __future__ module includes all the latest functions which were absent in the previous version of the Python this also includes the early release function which comes in handy for testing the new attribute of the code. By importing the __future__ module we are importing all the functions which are not compatible with the older version of the Python.

Import

In Python to access the function in the other module, we need to import the module in which the required function is available and this can be done by using the keyword ‘import’ following the name of the module.

However this is not the case here, we need to use “from ‘module-name’ import ‘item’ ” syntax.

Example

Here the print is the function for version 3.5.2 whereas in 2.7 the print was just a keyword.

#In 2.7 python compiler...
print("Hello","World", sep=' ... ', end='\n\n\n')

Output

File "Main.py", line 3
    print("Hello","World", sep=' ... ', end='\n\n\n')
                                    ^ 
SyntaxError: invalid syntax

Here we can see that there is an error occurred because the print is not the function in the 2.7 version of the Python.

and hence this will generate the error with the code.

Whereas in 3.5 compiler

#In python 3.5 Compiler
print("Hello","World", sep=' ... ', end='\n\n\n')

Output

Hello ... World

It worked!

To avoid this problem all we need is to use the __future__ module.

#In 2.7 python compiler
>>>from __future__ import print_function
>>>
>>>
>>>
>>>
>>>print
<built-in function print>

As we can see that print is now function in the 2.7 compiler too!

Also, read: Count the number of alphabets in a string in Python

 

Leave a Reply

Your email address will not be published. Required fields are marked *