Writing cross-compatible Python 2/3: Difference between __future__, six, and future.utils?

落爺英雄遲暮 提交于 2019-12-23 07:41:42

问题


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?


回答1:


In terms of python 2-3 compatibility:

__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 - 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 - more modern, only supports python>=2.6,>=3.3, and is more rich in features.

Seems like there is some agreement that future is preferred to six if you can drop support for old versions of python.



来源:https://stackoverflow.com/questions/42110826/writing-cross-compatible-python-2-3-difference-between-future-six-and-fut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!