编写交叉兼容的 Python 2/3:__future__、6 和 future.utils 之间的区别? [英] Writing cross-compatible Python 2/3: Difference between __future__, six, and future.utils?
问题描述
我正在此备忘单的帮助下编写交叉兼容的 Python 2 和 3 代码.我注意到有不同的包和模块可以帮助做到这一点:future
包(例如 future.utils
等),six
包,以及内置的 __future__
模块.
I'm writing cross-compatible Python 2 and 3 code with some help from this cheatsheet. I've noticed there are different packages and modules that help to do this: the future
package (e.g. future.utils
etc), the six
package, and the built in __future__
module.
在使用这些软件包时有什么需要注意的区别吗?我应该混合和匹配它们,还是可以编写完全交叉兼容的代码,只用其中之一编写?
Are there any differences to be aware of when using these packages? Should I be mixing and matching them, or is it possible to write fully cross-compatible code be written with just one of them?
推荐答案
在python 2-3兼容性方面:
In terms of python 2-3 compatibility:
__future__
- 是 python 中的一个内置模块,它允许您在 python 版本中使用可选功能,其中它们是可选的(与强制).例如,unicode_literals
在 python2.7
中是可选的,但在 python3.0
中成为必需的.
__future__
- is a built-in module in python which allows you to use optional features in python versions where they are optional (vs mandatory). For example, unicode_literals
was optional in python2.7
but became mandatory in python3.0
.
six
- 主要重命名模块/函数以在 python2
到 python3
之间产生更高的兼容性,但实际上并没有向后移植(或向前 -端口)功能.Python 版本 >=2.4
也支持它.
six
- mostly renames modules/functions to produce higher compatibility between python2
to python3
, but doesn't actually backport (or forward-port) functionality. It is also supported for python versions >=2.4
.
future
- 更现代,只支持python>=2.6,>=3.3
,功能更丰富.
future
- more modern, only supports python>=2.6,>=3.3
, and is more rich in features.
似乎有一些协议未来
如果您可以放弃对旧版本 python 的支持,则首选 six
.
Seems like there is some agreement that future
is preferred to six
if you can drop support for old versions of python.
这篇关于编写交叉兼容的 Python 2/3:__future__、6 和 future.utils 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!