Writing cross-compatible Python 2/3: Difference be

2019-06-17 02:17发布

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条回答
2楼-- · 2019-06-17 02:46

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.

查看更多
登录 后发表回答