__future__ Module in Python - GeeksforGeeks

17 Oct, 2021
__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. 
This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module. Its releases are in Python version 2.1. The basic idea of the __future__ module is to help migrate to use Python 3.X features. 
Note: The future statements must at the top of the file, otherwise the Python interpreter will raise SyntaxError
There are seven features in Python future module.
Python
import __future__print(__future__.all_feature_names)
Output:
['nested_scopes',  
'generators',
'division',
'absolute_import',
'with_statement',
'print_function',
'unicode_literals']
Example 1: 
The Python2 print statement is different from Python3 print function. We use the Python print statement in Python2 as:
print "Hello world"
But we can use the Python3 print function in the Python2 function using the future modules.
Python
# Code in Python 2from __future__ import print_function  print("Hello world")
Output:
Hello world
Example 2: 
Here we are going to print the message in Python 2.X with end attributes that come in Python 3 and “end” appends string in a newline. And it will raise an error because the function is not compatible with 2.x.
Python
# In 2.7 python compilerprint("Hello world", end=" ")
Output:
File "main.py", line 1
    print("Hello world", end=" ")
                            ^
SyntaxError: invalid syntax
So with __future__ print function we can import these features in our code to use the latest print function.
Python
# In 2.7 python compilerfrom __future__ import print_function  print("Hello world", end=";")
Output:
Hello world;
Example 3: 
sep also belongs to Python 3.x but here we will use these attributes by using this module. Let’s check these attributes without using the future modules.
Python3
# In 2.7 python compilerprint('Welcome ', ' Geeksforgeeks', sep = ' To ')
Output:
  File "main.py", line 1
    print('Welcome ', ' Geeksforgeeks', sep=' To ')
                                           ^
SyntaxError: invalid syntax
Then let’s use the __future__ print function to use the sep attributes.
Python
# In 2.7 python compilerfrom __future__ import print_functionprint('Welcome ', ' Geeksforgeeks', sep=' To ')
Output:
Welcome To Geeksforgeeks
Here we are going to use the division function in Python2.x and Python3.x.
Let’s see the example in Python2.x. 
Python
# In 2.7 python compilerprint 7 / 5   print -7 / 5
Output:
1.4
-1.4
And let see this with the future module, it will give you the accurate result.
Python
# In below python 2.x code, division works# same as Python 3.x because we use  __future__   from __future__ import division     print 7 / 5print -7 / 5
Output:
1.4
-1.4
In Python2.x we can not use Unicode but future modules allow us to use Unicode.
Example 1: 
In Python2 strings are considered bytes strings but in later versions, all strings are considered as a Unicode string.
Python
# code in Python2print(type("Geeks"))
Output:
<type 'str'>
Let use the future module in Python2.
Python
# code in Python2from __future__ import unicode_literals  print(type("Geeks"))
Output:
<type 'unicode'>
Example 2:
let’s see the example without future module, it will raise an error because we are building a byte string that holds UTF-8 encoded bytes
Python
# encoding: utf-8name = 'helló wörld from example'print name.encode('utf8')
Output:
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print name.encode('utf8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)
And it can be done in Python2X with future modules.
Python
# encoding: utf-8from __future__ import unicode_literalsname = 'helló wörld from example'print name.encode('utf8')
Output:
helló wörld from example
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python OOPs Concepts
How to Install PIP on Windows ?
Bar Plot in Matplotlib
Defaultdict in Python
Python Classes and Objects
Deque in Python
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python - Ways to remove duplicates from list
Class method vs Static method in Python