如何为 Python 3 做好准备?

发布于 2024-09-08 20:40:10 字数 619 浏览 8 评论 0原文

目前编写能够干净地通过 2to3 的 python 代码的规则是什么?这些实践似乎最适合编写不会永远陷入版本 2 中的代码。

我从 SciPy/NumPy 论坛上读到,“100% 测试覆盖率”(单元测试)对很多人来说都很重要,我也认为不确定这是否适用于所有人。当然,在转换后进行一组合理的单元测试来尝试您的代码似乎是一个明智的步骤。

还有其他事情吗?如果熟练的 Pythonista 正在编写他们希望在 2to3 过程中“干净”地完成的 2.x 代码,他们会做什么。

我正在寻找“[不]这样做”的具体实例以及一些更一般的“最佳实践”,但“该做和不该做”的具体实例很有帮助。

假设框架、库(Django、SciPy/NumPy)和我们需要的所有其他 C 扩展最终都移植到 Python3,我问的是您如何编写和维护您自己编写的纯 Python 语言代码。

更新:我真正想要的是“风格指南”和每个人都已经远离的已弃用功能列表。我在 Python 1.5 上开始学习,然后转向 2.0,然后并没有真正遵循 2.5/2.6 时代的大部分内容,使用它们,但实际上我的代码更多是 2.1 时代的。

What are the current rules for writing python code that will pass cleanly through 2to3 and what are the practices that seem to be best suited to writing code that will not become mired forever in version 2.

I have read from the SciPy/NumPy forums that "100% test coverage" (unit testing) is important for many people, and I am not sure if that would apply to everybody. Certainly having a reasonable set of unit tests to try your code out with after conversion, seems a sane step.

Are there other things? What are skilled Pythonistas doing if they are writing 2.x code that they hope to have come through "cleanly" in the 2to3 process.

I am looking for specific instances of "[don't] do this" as well as some more general "best-practices", but specific instances of "do's and don'ts" are helpful.

Let's assume that frameworks, libraries (Django, SciPy/NumPy), and every other C Extension we need gets ported to Python3 eventually, and I'm asking about how you write and maintain the pure python language code that you write yourself.

Update: It's possible that what I really want is the "style guide" and list of deprecated features that everybody was already staying away from. I cut my teeth on Python 1.5 and moved to 2.0, and then have not really followed much of the 2.5/2.6 era, used them but really my code is more 2.1 era.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-09-15 20:40:10

我会说:

  • 阅读“Python 3.0 的新增功能”。信息非常丰富。
    • 特别是,如果您关心 Unicode 或文本编码,请花些时间了解 3.x 发生了哪些变化。这可能是 Python 3.x 中最棘手的更改之一。
  • 获取 Python 2.6 或 2.7,并使用 -3 标志运行代码。它会告诉您代码中需要更改的内容。
  • 在使用第 3 方包之前,请检查它们是否具有 Python 3.x 版本。如果没有,请检查包网站、邮件列表、版本控制存储库等,了解包的开发活跃程度以及是否有支持 Python 3.x 的路线图。
  • 下载 Python 3.x 并尝试一下!诚然,如果您关心当前依赖于尚不支持 Python 3.x 的包(例如 wxPython 或 Django)的代码,那么这可能不切实际。

I'd say:

  • Read the "What's new for Python 3.0". Very informative.
    • In particular, if you care about Unicode or text encodings at all, take the time to understand what has changed for 3.x. That's probably one of the trickier things to change for Python 3.x.
  • Get Python 2.6 or 2.7, and run your code with the -3 flag. It will tell you about things in your code that will need changing.
  • Before using 3rd-party packages, check to see if they have a Python 3.x version. If not, check the package web site, mailing lists, version control repositories etc to see how actively the package is being developed and whether there is a roadmap towards Python 3.x support.
  • Download Python 3.x and try it out! Admittedly, that might not be practical if you care about code that currently depends on packages that don't yet support Python 3.x (e.g. wxPython or Django).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文