Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import all future features

Tags:

python

import

Just curious, I tried from __future__ import *, but I received this error:

  File "<stdin>", line 1
SyntaxError: future feature * is not defined

Well, that makes sense. A __future__ import is a little special and doesn't follow the normal rules, but it got me to thinking: how can I import all the future features?

like image 421
zondo Avatar asked Jul 29 '16 01:07

zondo


1 Answers

You can't, and that's by design. This is because more __future__ features might be added in the future, and those can break your code.

Imagine that in 2.x, the only __future__ feature was division. Then in 2.y, a new __future__ feature, print_function, is introduced. All of a sudden my code has broken:

from __future__ import *
print "Hello, World!"

You can, however, import __future__, and inspect its contents:

>>> import __future__
>>> [x for x in dir(__future__) if x.islower() and x[0] != '_']
['absolute_import', 'all_feature_names', 'division', 'generator_stop', 'generators', 'nested_scopes', 'print_function', 'unicode_literals', 'with_statement']

Note that these are not features and you should not try to import them. They instead describe which features are available, and from which versions they are.

like image 140
Fengyang Wang Avatar answered Oct 19 '22 23:10

Fengyang Wang