Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 510
Frank Avatar asked Mar 27 '12 21:03

Frank


People also ask

What is import__ future__ in Python?

__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module.

What does future do in Python?

future is the missing compatibility layer between Python 2 and Python 3. It allows you to use a single, clean Python 3. x-compatible codebase to support both Python 2 and Python 3 with minimal overhead.


2 Answers

Well, even if there wasn't documentation, __future__ is also a regular module that has some info about itself:

>>> import __future__
>>> __future__.all_feature_names
['nested_scopes', 'generators', 'division', 'absolute_import', 'with_statement', 'print_function', 'unicode_literals']
>>> __future__.unicode_literals
_Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 131072)

Python 2.6 has most of the features already enabled, so choose from division, print_function, absolute_import and unicode_literals.

And no, import __future__ won't work as you think. It's only magic when you use the from __future__ import something form as the first statement in the file. See the docs for more.

Of course, no matter how much you import from __future__, you will get different behavior in 3.x.

like image 59
Petr Viktorin Avatar answered Oct 22 '22 11:10

Petr Viktorin


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.

like image 38
Karl Knechtel Avatar answered Oct 22 '22 11:10

Karl Knechtel