为什么 Python 3 不向后兼容?

发布于 2024-12-29 17:12:51 字数 103 浏览 0 评论 0原文

我了解到 Python 3 不向后兼容。

它不会影响很多使用旧版本Python的应用程序吗?

Python 3 的开发者为什么不认为绝对有必要使其向后兼容呢?

I have learned that Python 3 is not backwards compatible.

Will it not affect a lot of applications using older versions of Python?

How did the developers of Python 3 not think it was absolutely necessary to make it backwards compatible?

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

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

发布评论

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

评论(1

梦在深巷 2025-01-05 17:12:52

Python 3.0 向后兼容吗?为什么?

Python 3.0 实现了很多非常有用的功能,但破坏了向后兼容性。它是有意这样做的,因此即使 Python 2.x 代码在 Python 3.x 下可能无法正常工作,也可以实现出色的功能。

因此,基本上,Python 3.0 并不是故意向后兼容的因此,您可以从一组全新的功能中受益。它甚至被称为“Python 3000”或“Python 3K”。

来自“Python 3.0 中的新增功能”(可用 在这里):

Python 3.0 与 2.6 相比。 Python 3.0,也称为“Python 3000”或“Py3K”,是第一个故意向后不兼容的 Python 版本。与典型版本相比,其中的变化更多,而且对所有 Python 用户来说也更重要。然而,在消化了这些变化之后,你会发现 Python 确实没有改变那么多——总的来说,我们主要是修复众所周知的烦恼和缺陷,并删除许多旧的东西.


Python 3.0 中的新功能打破了向后兼容性

一些最显着的功能可能被认为打破了向后兼容性,但同时改进了语言,它们是:

  • print 现在是一个函数,而不是一个语句,并将其用作语句会导致错误,
  • 各种函数&方法现在返回迭代器或视图而不是列表,这使得迭代其结果更加节省内存(您不需要将整个结果列表存储在内存中),
  • 用于排序函数的 cmp 参数像 sorted()list.sort() 不再支持,应该用 key 参数替换,
  • int 现在与 Python 2.x 相同long,这使得数字处理变得不那么复杂,
  • / 运算符现在默认是真正除法的运算符(您仍然可以使用 // 来表示底数)除法),
  • Python 3.x 中的文本现在默认为 Unicode,
  • TrueFalseNone 现在是保留字(因此您不需要能够做到True, False = False, True
  • 改变了用法元类中,
  • 异常需要从 BaseException 派生,必须以与 Python 2.x 不同的方式引发和捕获,
  • 以及更多其他更改,使 Python 更具可读性、一致性和一致性。明确的,

Is Python 3.0 backward-compatible and why?

Python 3.0 implements a lot of very useful features and breaks backward compatibility. It does it on purpose, so the great features can be implemented even despite the fact Python 2.x code may not work correctly under Python 3.x.

So, basically, Python 3.0 is not backward-compatible on purpose. Thanks to that, you can benefit from a whole new set of features. It is even called "Python 3000" or "Python 3K".

From "What's new in Python 3.0" (available here):

Python 3.0, compared to 2.6. Python 3.0, also known as “Python 3000” or “Py3K”, is the first ever intentionally backwards incompatible Python release. There are more changes than in a typical release, and more that are important for all Python users. Nevertheless, after digesting the changes, you’ll find that Python really hasn’t changed all that much – by and large, we’re mostly fixing well-known annoyances and warts, and removing a lot of old cruft.

Python features new in 3.0, breaking backward compatibility

Some of the most notable features that may be considered as breaking backward compatibility, but improving the language at the same time, are:

  • print is now a function, not a statement, and using it as statement will result in an error,
  • various functions & methods now return an iterator or view instead of list, which makes iterating through their results more memory-efficient (you do not need to store the whole list of results in the memory),
  • cmp argument for sorting functions like sorted() and list.sort() is no longer supported, and should be replaced by key argument,
  • int is now the same as Python 2.x's long, which makes number processing less complex,
  • / operator is now an operator for true division by default (you can still use // for floor division),
  • text in Python 3.x is now Unicode by default,
  • True, False and None are now reserved words (so you are not able to do True, False = False, True,
  • changed usage of metaclass,
  • exceptions are required to be derived from BaseException, must be raised & caught differently than in Python 2.x,
  • and a lot more other changes, making Python more readable, consistent & explicit,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文