Tips on upgrading to python 3.0?
因此,随着python 3.0(现在是3.1)的最终版本的发布,很多人都面临着如何在不因向后不兼容而损失一半代码库的情况下进行升级的问题。
为了避免切换到下一代python几乎不可避免地带来的许多陷阱,人们最好的建议是什么?
可能一个好的开始是"使用2to 3将python 2.x代码转换为3.x":-)
我写了一本关于这个的免费书。您可以在这里阅读:
网址:http://python3porting.com/
简而言之:
- 如果您真的想要整数除法,请使用//。
- 确保在打开二进制文件时用"b"标记将其标记为指示数据是否为二进制。
小精灵
差不多就是这样。
首先,这个问题非常类似于您计划如何处理到Python3的迁移?.检查那里的答案。
在python wiki中还有一节关于将应用程序移植到python 3.x
python 3.0的发行说明包含一个关于移植的部分。我引用了这里的提示:
(Prerequisite:) Start with excellent test coverage. Port to Python 2.6. This should be no more work than the average por
from Python 2.x to Python 2.(x+1).
Make sure all your tests pass.(Still using 2.6:) Turn on the -3 command line switch. This enables warnings about features that will be
removed (or change) in 3.0. Run your
test suite again, and fix code that
you get warnings about until there are
no warnings left, and all your tests
still pass.Run the 2to3 source-to-source translator over your source code tree.
(See 2to3 - Automated Python 2 to 3
code translation for more on this
tool.) Run the result of the
translation under Python 3.0. Manually
fix up any remaining issues, fixing
problems until all tests pass again.It is not recommended to try to write
source code that runs unchanged under
both Python 2.6 and 3.0; you’d have to
use a very contorted coding style,
e.g. avoiding print statements,
metaclasses, and much more. If you are
maintaining a library that needs to
support both Python 2.6 and Python
3.0, the best approach is to modify step 3 above by editing the 2.6
version of the source code and running
the 2to3 translator again, rather than
editing the 3.0 version of the source
code.
号
如果没有一个真正令人信服的升级理由,我会坚持什么是有效的。我看了一下我每天使用的脚本的升级,它的工作太多,我看不到任何好处。
"如果还没坏,就别修了!"