Sunday, December 29, 2024
Google search engine
HomeLanguages__future__ Module in Python

__future__ Module in Python

__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

Following features in this module:

Features Optional in Mandatory in
nested_scopes 2.1 2.2
generators 2.2 2.3
division 2.2 3.0
absolute_import 2.5 3.0
with_statement 2.5 2.6
print_function 2.6 3.0
unicode_literals 2.6 3.0
generator_stop 3.5 3.7
annotations 3.7 3.11

Basic features in future module:

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']

__future__ module with print_function

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 2
from __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 compiler
print("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 compiler
from __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 compiler
print('Welcome ', ' GeeksforLazyroar', sep = ' To ')


Output:

  File "main.py", line 1
    print('Welcome ', ' GeeksforLazyroar', sep=' To ')
                                           ^
SyntaxError: invalid syntax

Then let’s use the __future__ print function to use the sep attributes.

Python




# In 2.7 python compiler
from __future__ import print_function
print('Welcome ', ' GeeksforLazyroar', sep=' To ')


Output:

Welcome To GeeksforLazyroar

__future__  module with division function

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 compiler
print 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 / 5
print -7 / 5


Output:

1.4
-1.4

__future__  module with unicode_literals function

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 Python2
print(type("Geeks"))


Output:

<type 'str'>

Let use the future module in Python2.

Python




# code in Python2
from __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-8
name = 'helló wö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-8
from __future__ import unicode_literals
name = 'helló wörld from example'
print name.encode('utf8')


Output:

helló wörld from example

RELATED ARTICLES

Most Popular

Recent Comments