Skip to Content
《学习 Python》第 5 版
book

《学习 Python》第 5 版

by Mark Lutz
May 2025
Intermediate to advanced
1648 pages
23h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 《学习 Python》第 5 版

附录 C. Python 变化与本书

本附录 简要总结了最近发布的 Python 中的更改,这些更改按首次出现的书籍版本进行了分类,并给出了本书中这些更改的链接。该附录旨在为以前版本的读者以及从以前的 Python 版本迁移过来的开发人员提供参考。

以下是 Python 的变化与本书近期版本的关系:

  • 2013 年第五版涵盖 Python 3.3 和 2.7。

  • 2009 年第四版涵盖了 Python 2.6 和 3.0(部分 3.1 功能)。

  • 2007 年第三版涉及 Python 2.5。

  • 1999 年和 2003 年的第一版第二版涉及 Python 2.0 和 2.2。

  • 本书的前身是 1996 年出版的《Python 编程》,内容涵盖 Python 1.3。

因此,要查看第五版中的更改,请参阅前面列出的 Python 2.7、3.2 和 3.3 更改。对于第四版和第五版(即从第三版开始) 中的更改,也请参见 Python 2.6、3.0 和 3.1 的更改。这里也简要列出了第三版的语言更改,不过这在今天似乎只具有历史价值。

还要注意的是,本附录侧重于主要更改和对书籍的影响,并不打算作为 Python 演进的完整指南。要想更全面地了解 Python 每个新版本的变化,请查阅 "What's New "文档,这些文档是其标准文档集的一部分,可在python.org 的 Documentation 页面获取。第 15 章介绍了 Python 文档及其手册集。

2.X/3.X 的主要区别

本附录的大部分内容与 Python 对书籍覆盖范围的修改有关。如果您想快速了解最突出的 2.X/3.X 区别,下面的内容就足够了。请注意,本节主要比较了最新的 3.X 和 2.X 版本--3.3 和 2.7。这里没有列出许多 3.X 特性,因为它们要么也被添加到了 2.6 中(如with 语句和类装饰器),要么后来被回移植到了 2.7 中(如集合和字典理解),但在早期的 2.X 版本中却不可用。有关早期版本中更改的更细粒度的信息,请参见后面的章节,有关未来版本中可能出现的更改,请参见 Python 的 "What's New "文档。

3.X 差异

下面总结了 Python 产品线中不同的工具。

  • Unicode 字符串模型:在 3.X 中,正常的str 字符串支持包括 ASCII 在内的所有 Unicode 文本,单独的bytes 类型表示原始的 8 位字节序列。在 2.X 中,正常的str字符串支持包括 ASCII 码在内的 8 位文本,而单独的unicode 类型则表示更丰富的 Unicode 文本(可选)。

  • 文件模型:在 3.X 中,由open 创建的文件因内容而异--文本文件采用 Unicode 编码,以str 字符串表示内容,二进制文件则以bytes字符串表示内容。在 2.X 中,文件使用不同的接口--由open 创建的文件以str 字符串表示 8 位文本或基于字节的数据内容,而codecs.open 实现了 Unicode 文本编码。

  • 类模型:在 3.X 中,所有类都自动从object 派生,并获得新式类的大量更改和扩展,包括不同的继承算法、内置调度和菱形树的 MRO 搜索顺序。在 2.X 中,普通类遵循经典模式,而从object 或其他内置类型的显式继承则使新式模式成为可选项。

  • 内置可迭代对象在 3.X 中,map,zip,range,filter 和 dictionarykeys,values, 以及items 都是可迭代对象,可根据请求生成值。在 2.X 中,这些调用会创建物理列表。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

学习 Java,第 6 版

学习 Java,第 6 版

Marc Loy, Patrick Niemeyer, Daniel Leuck
《高性能 Python》第二版

《高性能 Python》第二版

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9798341656963