是否可以从 python 代码中删除所有 __future__ 语句,而不影响其使用 python 3.7.1 的功能?
Can all __future__ statements be removed from python code, without affecting its functionality using python 3.7.1?
如果我使用 python 3.7.1,是否可以从源代码中删除以下 __future__
语句而不影响其功能?
from __future__ import nested_scopes
from __future__ import generators
from __future__ import division
from __future__ import absolute_import
from __future__ import with_statement
from __future__ import print_function
from __future__ import unicode_literals
您可以删除 那些 __future__
导入而不影响功能,但删除它们是不必要的,并且会停止与早期 python 版本的兼容性。
此外,正如@deceze 在他的评论中提到的那样,其他 导入可能不同。例如,from __future__ import annotations
通过导入在 Python <= 4.0 中是 only enabled,因此 adding/removing 该行会影响功能:
Since this change breaks compatibility, the new behavior needs to be
enabled on a per-module basis in Python 3.7 using a __future__
import:
from __future__ import annotations
It will become the default in Python 4.0.
正如@jmd_dk 指出的那样,您实际上可以在 __future__
模块中找到此信息。我写了一个简短的脚本来提取它:
import __future__
import ast
import sys
print('Python version:', sys.version_info)
sys_t = sys.version_info[:3]
s = '__future__ import {} {} for you; the version: {} vs. your version: {}'
for name in __future__.all_feature_names:
optional, mandatory, _ = ast.literal_eval(str(getattr(__future__, name)).lstrip('_Featur'))
optional, mandatory = optional[:3], mandatory[:3]
print('\nName: {}'.format(name))
tmp = [None, None, optional, sys_t]
if optional <= sys_t:
tmp[:2] = 'is', 'included'
else:
tmp[:2] = 'not', 'included'
print(s.format(*tmp))
tmp[2] = mandatory
if mandatory <= sys_t:
tmp[:2] = 'is', 'fixed'
else:
tmp[:2] = 'not', 'fixed'
print(s.format(*tmp))
在我的系统上输出:
Python version: sys.version_info(major=3, minor=7, micro=1, releaselevel='final', serial=0)
Name: nested_scopes
__future__ import is included for you; the version: (2, 1, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (2, 2, 0) vs. your version: (3, 7, 1)
Name: generators
__future__ import is included for you; the version: (2, 2, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (2, 3, 0) vs. your version: (3, 7, 1)
Name: division
__future__ import is included for you; the version: (2, 2, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: absolute_import
__future__ import is included for you; the version: (2, 5, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: with_statement
__future__ import is included for you; the version: (2, 5, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (2, 6, 0) vs. your version: (3, 7, 1)
Name: print_function
__future__ import is included for you; the version: (2, 6, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: unicode_literals
__future__ import is included for you; the version: (2, 6, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: barry_as_FLUFL
__future__ import is included for you; the version: (3, 1, 0) vs. your version: (3, 7, 1)
__future__ import not fixed for you; the version: (3, 9, 0) vs. your version: (3, 7, 1)
Name: generator_stop
__future__ import is included for you; the version: (3, 5, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 7, 0) vs. your version: (3, 7, 1)
Name: annotations
__future__ import is included for you; the version: (3, 7, 0) vs. your version: (3, 7, 1)
__future__ import not fixed for you; the version: (4, 0, 0) vs. your version: (3, 7, 1)
当 Python >= 3.8 引入 __future__
导入(在我写这篇文章时还有 none),删除 这些 和 运行 on Python 3.7 显然会影响功能。
这在lib/python3.7/__future__.py
file. Each future import (here called _Feature
) is given two 5-tuples, specifying optional and mandatory releases, respectively. Here, "mandatory release" means which version of Python includes the feature by default. As you can see by following the link above, all mandatory versions are < 3.7.1 except two, namely barry_as_FLUFL
(mandatory in version 3.9.0) and annotations
(4.0.0 版强制)中有记录,其中第一个只是一个复活节彩蛋。
如果使用 Python 3.7.1 或更新版本,您列表中的所有未来导入确实可以被删除,并保证完全相同的代码执行。正如其他人评论的那样,这可能不是一个好主意,因为这会降低代码兼容性。
如果我使用 python 3.7.1,是否可以从源代码中删除以下 __future__
语句而不影响其功能?
from __future__ import nested_scopes
from __future__ import generators
from __future__ import division
from __future__ import absolute_import
from __future__ import with_statement
from __future__ import print_function
from __future__ import unicode_literals
您可以删除 那些 __future__
导入而不影响功能,但删除它们是不必要的,并且会停止与早期 python 版本的兼容性。
此外,正如@deceze 在他的评论中提到的那样,其他 导入可能不同。例如,from __future__ import annotations
通过导入在 Python <= 4.0 中是 only enabled,因此 adding/removing 该行会影响功能:
Since this change breaks compatibility, the new behavior needs to be enabled on a per-module basis in Python 3.7 using a
__future__
import:
from __future__ import annotations
It will become the default in Python 4.0.
正如@jmd_dk 指出的那样,您实际上可以在 __future__
模块中找到此信息。我写了一个简短的脚本来提取它:
import __future__
import ast
import sys
print('Python version:', sys.version_info)
sys_t = sys.version_info[:3]
s = '__future__ import {} {} for you; the version: {} vs. your version: {}'
for name in __future__.all_feature_names:
optional, mandatory, _ = ast.literal_eval(str(getattr(__future__, name)).lstrip('_Featur'))
optional, mandatory = optional[:3], mandatory[:3]
print('\nName: {}'.format(name))
tmp = [None, None, optional, sys_t]
if optional <= sys_t:
tmp[:2] = 'is', 'included'
else:
tmp[:2] = 'not', 'included'
print(s.format(*tmp))
tmp[2] = mandatory
if mandatory <= sys_t:
tmp[:2] = 'is', 'fixed'
else:
tmp[:2] = 'not', 'fixed'
print(s.format(*tmp))
在我的系统上输出:
Python version: sys.version_info(major=3, minor=7, micro=1, releaselevel='final', serial=0)
Name: nested_scopes
__future__ import is included for you; the version: (2, 1, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (2, 2, 0) vs. your version: (3, 7, 1)
Name: generators
__future__ import is included for you; the version: (2, 2, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (2, 3, 0) vs. your version: (3, 7, 1)
Name: division
__future__ import is included for you; the version: (2, 2, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: absolute_import
__future__ import is included for you; the version: (2, 5, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: with_statement
__future__ import is included for you; the version: (2, 5, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (2, 6, 0) vs. your version: (3, 7, 1)
Name: print_function
__future__ import is included for you; the version: (2, 6, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: unicode_literals
__future__ import is included for you; the version: (2, 6, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 0, 0) vs. your version: (3, 7, 1)
Name: barry_as_FLUFL
__future__ import is included for you; the version: (3, 1, 0) vs. your version: (3, 7, 1)
__future__ import not fixed for you; the version: (3, 9, 0) vs. your version: (3, 7, 1)
Name: generator_stop
__future__ import is included for you; the version: (3, 5, 0) vs. your version: (3, 7, 1)
__future__ import is fixed for you; the version: (3, 7, 0) vs. your version: (3, 7, 1)
Name: annotations
__future__ import is included for you; the version: (3, 7, 0) vs. your version: (3, 7, 1)
__future__ import not fixed for you; the version: (4, 0, 0) vs. your version: (3, 7, 1)
当 Python >= 3.8 引入 __future__
导入(在我写这篇文章时还有 none),删除 这些 和 运行 on Python 3.7 显然会影响功能。
这在lib/python3.7/__future__.py
file. Each future import (here called _Feature
) is given two 5-tuples, specifying optional and mandatory releases, respectively. Here, "mandatory release" means which version of Python includes the feature by default. As you can see by following the link above, all mandatory versions are < 3.7.1 except two, namely barry_as_FLUFL
(mandatory in version 3.9.0) and annotations
(4.0.0 版强制)中有记录,其中第一个只是一个复活节彩蛋。
如果使用 Python 3.7.1 或更新版本,您列表中的所有未来导入确实可以被删除,并保证完全相同的代码执行。正如其他人评论的那样,这可能不是一个好主意,因为这会降低代码兼容性。