4

In Python 2.7 using the __future__ module and the print_function you can use the python 3.X print function. My question is, how did the developers of Python know what was coming in the future releases of Python? Or was this module added to Python 2.7 after it was released? Here is the code I am talking about:

from __future__ import print_function
print("Hello world!")
Mark Skelton
  • 3,663
  • 4
  • 27
  • 47
  • 2
    You might be interested by this article: [How Does 'from \_\_future__ import ...' Work in Python?](http://simeonvisser.com/posts/how-does-from-future-import-work-in-python.html) – Vincent Savard Feb 17 '16 at 17:47

1 Answers1

3

The __future__ module was introduced in Python 2.1 in order to have access to upcoming features/functions which will lead to incompatibilities with the current implementation and is extended with each version if needed.

So the module gives the possibility to use those incompatible functions of future versions in earlier versions. So you can make use of the upcoming advantages of those functions.

There are three main reasons for that module as stated in the docs:

__future__ is a real module, and serves three purposes:

  • To avoid confusing existing tools that analyze import statements and expect to find the modules they’re importing.
  • To ensure that future statements run under releases prior to 2.1 at least yield runtime exceptions (the import of __future__ will fail, because there was no module of that name prior to 2.1).
  • To document when incompatible changes were introduced, and when they will be — or were — made mandatory. This is a form of executable documentation, and can be inspected programmatically via importing __future__ and examining its contents.
albert
  • 8,027
  • 10
  • 48
  • 84