What is __future__ in Python used for and how/when to use it, and how it works
__future__ frequently appears in Python modules. I do not understand what
__future__ is for and how/when to use it even after reading the Python's
Can anyone explain with examples?
A few answers regarding the basic usage of
__future__ I've received seemed correct.
However, I need to understand one more thing regarding how
The most confusing concept for me is how a current python release includes features for future releases, and how a program using a feature from a future release can be be compiled successfully in the current version of Python.
I am guessing that the current release is packaged with potential features for the future. However, the features are available only by using
__future__ because they are not the current standard. Let me know if I am right.
__future__ module's inclusion, you can slowly be accustomed to incompatible changes or to such ones introducing new keywords.
E.g., for using context managers, you had to do
from __future__ import with_statement in 2.5, as the
with keyword was new and shouldn't be used as variable names any longer. In order to use
with as a Python keyword in Python 2.5 or older, you will need to use the import from above.
Another example is
from __future__ import division print 8/7 # prints 1.1428571428571428 print 8//7 # prints 1
__future__ stuff, both
The internal difference is that without that import,
/ is mapped to the
__div__() method, while with it,
__truediv__() is used. (In any case,
>>> print >>> from __future__ import print_function >>> print <built-in function print> >>>
Read more... Read less...
When you do
from __future__ import whatever
You're not actually using an
import statement, but a future statement. You're reading the wrong docs, as you're not actually importing that module.
Future statements are special -- they change how your Python module is parsed, which is why they must be at the top of the file. They give new -- or different -- meaning to words or symbols in your file. From the docs:
A future statement is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python. The future statement is intended to ease migration to future versions of Python that introduce incompatible changes to the language. It allows use of the new features on a per-module basis before the release in which the feature becomes standard.
If you actually want to import the
__future__ module, just do
and then access it as usual.
__future__ is a pseudo-module which programmers can use to enable new language features which are not compatible with the current interpreter. For example, the expression
11/4 currently evaluates to
2. If the module in which it is executed had enabled true division by executing:
from __future__ import division
11/4 would evaluate to
2.75. By importing the
__future__ module and evaluating its variables, you can see when a new feature was first added to the language and when it will become the default:
>>> import __future__ >>> __future__.division _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
It can be used to use features which will appear in newer versions while having an older release of Python.
>>> from __future__ import print_function
will allow you to use
>>> print('# of entries', len(dictionary), file=sys.stderr)
There are some great answers already, but none of them address a complete list of what the
__future__ statement currently supports.
Put simply, the
__future__ statement forces Python interpreters to use newer features of the language.
The features that it currently supports are the following:
Prior to Python 2.1, the following code would raise a NameError:
def f(): ... def g(value): ... return g(value-1) + 1 ...
from __future__ import nested_scopes directive will allow for this feature to be enabled.
Introduced generator functions such as the one below to save state between successive function calls:
def fib(): a, b = 0, 1 while 1: yield b a, b = b, a+b
Classic division is used in Python 2.x versions. Meaning that some division statements return a reasonable approximation of division ("true division") and others return the floor ("floor division"). Starting in Python 3.0, true division is specified by
x/y, whereas floor division is specified by
from __future__ import division directive forces the use of Python 3.0 style division.
Allows for parenthesis to enclose multiple
import statements. For example:
from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text, LEFT, DISABLED, NORMAL, RIDGE, END)
from Tkinter import Tk, Frame, Button, Entry, Canvas, Text, \ LEFT, DISABLED, NORMAL, RIDGE, END
from Tkinter import Tk, Frame, Button, Entry, Canvas, Text from Tkinter import LEFT, DISABLED, NORMAL, RIDGE, END
Adds the statement
with as a keyword in Python to eliminate the need for
try/finally statements. Common uses of this are when doing file I/O such as:
with open('workfile', 'r') as f: read_data = f.read()
Forces the use of Python 3 parenthesis-style
print() function call instead of the
print MESSAGE style statement.
Introduces the literal syntax for the
bytes object. Meaning that statements such as
bytes('Hello world', 'ascii') can be simply expressed as
Replaces the use of the
StopIteration exception used inside generator functions with the
One other use not mentioned above is that the
__future__ statement also requires the use of Python 2.1+ interpreters since using an older version will throw a runtime exception.
Or is it like saying "Since this is python v2.7, use that different 'print' function that has also been added to python v2.7, after it was added in python 3. So my 'print' will no longer be statements (eg print "message" ) but functions (eg, print("message", options). That way when my code is run in python 3, 'print' will not break."
from __future__ import print_function
print_function is the module containing the new implementation of 'print' as per how it is behaving in python v3.
This has more explanation: http://python3porting.com/noconv.html