Python – What is the __future__ module?

I was asked an interesting Python question recently: what is the __future__ module?

If the __future__ (PEP that introduced __future__) module seems vaguely familiar to you, it is because I use it in most of my Python tutorial scripts and have briefly talked about __future__ in a few of my tutorials, but I have never really explained what it is.

You know how you must import sys in order to access the functions located in the sys module? The same goes for accessing the special items located in __future__. However, you cannot simply import the __future__ module like any other module. That is because __future__ backports standard functionality in future Python versions. Because making them standard at in a typical patch release may create breaking change (and breaking changes should never be made in a patch release), the Python developers instead a reference to the new feature in __future__ and waited to introduce the new standard behavior in a minor- or if it is a highly volatile change, major- release.

By inserting a reference to the new changes in __future__ and withholding making them standard until the appointed version prevent substantial breaking changes, it also permitted developers early access to the changes, thereby allowing them to update any code that may/would break or benefit from the coming changes. For example, both the with statement and Python 3 print() function was initially made available through __future__. Python 2 developers often import __division__ to gain true division, something not made standard until Python 3.0.

In all cases, if the Python version being used supports the feature or change without needed __future__, the import is ignored. This is good in the chance the code supports both Python 2 and 3.

To access the content in __future__, use the from module import item syntax. You cannot use the “normal” import module and dot notation method, as nothing it references can be used in that manner, nor will from module import * work (“SyntaxError: future feature * is not defined“). Any __future__ imports must be the first imports made. Importing __future__ code after importing other modules will again raise a SyntaxError (“from __future__ imports must occur at the beginning of the file“).

To quickly demonstrate using __future__ in a script, we will write a Python 2 and 3 supporting script that rolls a set number of dice (however many dice is defined by the numOfDice variable) that for some reason must make use of the end parameter of the Python 3 print() function, which recall is only available in Python 2 by importing it from __future__ (exactly why the script is written this way stems the author’s lack of ideas, the fact this use case has been covered before in a previous tutorial, and a limited number of currently relevant __future__ imports that do not require using an archaic version of Python šŸ˜› ).

If you were to comment out the __future__ import and run that script on Python 2, you would encounter a SyntaxError because you do not have access to the New and Improved Print Function™

And that is __future__ in a nutshell. šŸ™‚


Triangular Reactions

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s