__future__ Module in Python
Last Updated :
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
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
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
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
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
print ( '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
from __future__ import print_function
print ( 'Welcome ' , ' Geeksforgeeks' , sep = ' To ' )
|
Output:
Welcome To Geeksforgeeks
__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.
Output:
1.4
-1.4
And let see this with the future module, it will give you the accurate result.
Python
from __future__ import division
print 7 / 5
print - 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.
Output:
<type 'str'>
Let use the future module in Python2.
Python
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
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
from __future__ import unicode_literals
name = 'helló wörld from example'
print name.encode( 'utf8' )
|
Output:
helló wörld from example
Similar Reads
Python Fire Module
Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read
Arrow module in Python
Arrow is a Python module for working with date and time. It offers a sensible and human-friendly approach to creating, manipulating, formatting and converting dates, times and timestamps. It allows easy creation of date and time instances with timezone awareness.  Installation The arrow module is i
4 min read
Built-in Modules in Python
Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
Import module in Python
In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
External Modules in Python
Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
Python Module Index
Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Python datetime module
In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python
14 min read
Sched module in Python
Sched module is the standard library, can be used in the creation of bots and other monitoring and automation applications. The sched module implements a generic event scheduler for running tasks at specific times. It provides similar tools like task scheduler in windows or Linux, but the main advan
4 min read
Python Modules
Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work. In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we
7 min read
How to create modules in Python 3 ?
Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t
4 min read