1

As explained in another post(https://stackoverflow.com/a/7075121/2706606), in python you can import stuff from the future.

For example 2/4 evaluates to 0 (as it is an integer division).

>>>print 2/4
0

But if you import "division" from the future, it evaluates to 0.5

>>> from __future__ import division
>>> print 2/4
0.5

What I understand from the linked post is that the parser parses the code differently when you use future imports.

And my question is, is there only one single, unique "future" environment where all this future imports are fetched from?

How do we guarantee that the future imports will be forward-compatible with future versions of the python compiler?

Is it possible that maybe in the "more distant future" 2/4 will evaluate to some other value?

Community
  • 1
  • 1
Gokay
  • 758
  • 5
  • 9
  • `__future__` is use only in Python 2 which is almost dead. Nobody will need this in the future. – furas Nov 04 '16 at 01:38
  • Yes. The future environment is called Python 3. – Sumner Evans Nov 04 '16 at 01:41
  • 2
    @furas: Absolutely not. It's used in Py3 for new things even now, for `generator_stop` ([ref](https://docs.python.org/3/library/__future__.html)). Once the future arrives though, the imports stop doing anything; they're recognized, but ignored (because the default syntax already includes them). If you import something that doesn't exist yet in your version of Python (e.g. `with_statement` being imported in 2.4), you'd get an exception that lets you know the code is incompatible with that old version of Python. – ShadowRanger Nov 04 '16 at 01:46

2 Answers2

2

The features from the future are not going to change meaning. It is guaranteed that from __future__ import x has one of three behaviors:

  1. On a version of Python before the future behavior was defined, an exception is thrown indicating the code is incompatible
  2. On a version of Python before the behavior becomes the default, it enables the behavior
  3. On a version of Python after the behavior becomes the default, the import is recognized, but ignored

They will not reuse a name to mean something else later on (since that would violate the "recognized but ignored" rule for #3). If they decided to change, say, division again, they'd need to use a new name, e.g. to make it truncate towards zero division instead of floor division, they might make a __future__ import named truncating_division. But they won't delete division, ever, as long as Python exists, nor will they change the meaning, per the __future__ docs:

No feature description will ever be deleted from __future__.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
1

If you look at the import __future__ there is a list called all_feature_names that you can use to see what features are available for your python interpreter.

import __future__
print(__future__.all_feature_names)

Which on my machine would return the following on Python 2.7.12.

['nested_scopes', 'generators', 'division', 'absolute_import',
 'with_statement', 'print_function', 'unicode_literals']

And with Python 3.5.2.

['nested_scopes', 'generators', 'division', 'absolute_import', 
 'with_statement', 'print_function', 'unicode_literals', 
 'barry_as_FLUFL', 'generator_stop']

There is however no guarantee that the Python interpreter you are running will have the same features available, the import may not even exist on some implementations, but assuming that the feature is available the implementation should remain the same, as long as it was implemented according to the PEP (e.g. PEP-0238), which should be the case for most Python interpreters.

eandersson
  • 25,781
  • 8
  • 89
  • 110