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.
Well, even if there wasn’t documentation,
__future__is also a regular module that has some info about itself:Python 2.6 has most of the features already enabled, so choose from
division,print_function,absolute_importandunicode_literals.And no,
import __future__won’t work as you think. It’s only magic when you use thefrom __future__ import somethingform 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.