Alex Rivera | Logout

What __future__ features should I import in Python v2.6.2?

Asked 2012-03-27T21:33:18.970
9

I am starting to learn Python, but I'm forced to use a v2.6.2 interpreter.

I want to get as close as possible to Python 3, e.g, using the new print function, "true" division, etc.

from __future__ import division
from __future__ import print_function
print(1/2, file=sys.stderr) # 0.5

What other features should I import from __future__?

I guess I could do a general import __future__ but then I would get different behavior when I upgrade to a higher version (v2.7 might have more features in __future__), and my scripts might stop working then.

Edit
Report

1 Answer

7

What other features should I import from __future__?

To get the most up-to-date behaviour, you should of course import every __future__ feature that's offered, except the ones that you get anyway. (The way the system is set up, old features are not dropped even after they're always-on.)

Note that import __future__ will not give you everything, and neither will from __future__ import *. The from ... import ... syntax is special-cased for __future__ (which is how it works), but __future__ is still a real module that you can import with import __future__. However, doing so will let you know the actual feature names, as well as information on when they were (or are expected to be) made default, and when they became available.

>>> [
...     name for name in __future__.all_feature_names if
...     getattr(__future__, name).optional <=
...     sys.version_info <
...     getattr(__future__, name).mandatory
... ]

['division', 'print_function', 'unicode_literals']

is what I get on 2.7.2.

answered 2012-03-27T21:56:01.750

Your Answer