__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
# 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 ', ' 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 compiler
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.
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
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
Similar Reads
Functools module in Python
Functools module is for higher-order functions that work on other functions. It provides functions for working with other functions and callable objects to use or extend them without completely rewriting them. This module has two classes - partial and partialmethod. Partial class A partial function
6 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
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
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
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
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 Math Module
Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks. The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric
13 min read
Inspect Module in Python
The inspect module in Python is useful for examining objects in your code. Since Python is an object-oriented language, this module helps inspect modules, functions and other objects to better understand their structure. It also allows for detailed analysis of function calls and tracebacks, making d
4 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 | Calendar Module
Python defines an inbuilt module calendar that handles operations related to the calendar. In this article, we will see the Python Program to Display Calendar. Calendar Module in PythonThe calendar module allows us to output calendars like the program and provides additional useful functions related
5 min read