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 (eg 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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.