继续上次的工作,今天的翻译内容主要涉及4版和3版的新增章节。并且将会提及2.6和3.0中的改变和新特性。
前言(续)
新添加的章节
Although the main purpose of this edition is to update the examples and material from the preceding edition for 3.0 and 2.6, I’ve also added five new chapters to address new topics and add context:
尽管这一版的主要目的是为了更新以前版本的例子和材料使之适应3.0和2.6,但我同时也新写了章节,在其中提及新的主题和内容。
- Chapter 27 is a new class tutorial, using a more realistic example to explore the basics of Python object-oriented programming (OOP).
- Chapter 36 provides details on Unicode and byte strings and outlines string and file differences between 3.0 and 2.6.
- Chapter 37 collects managed attribute tools such as properties and provides new coverage of descriptors.
- Chapter 38 presents function and class decorators and works through comprehensive examples.
- Chapter 39 covers metaclasses and compares and contrasts them with decorators.
- 第27章是新类的手册,增加了更实际的例子使读者更好地探索Python的面向对象编程基础。
- 第36章详细介绍了Unicode和byte字符串,同时对字符串和文件在3.0和2.6中的不同提供了一个概述。
- 第37章收集了一些属性管理工具。这些工具涉及性能并提供了一个新的描述。
- 第38章通过全面的实例讲述了函数和类描述符。
- 第39章涵盖了原类,同时将原类和类描述符进行了比较。
The first of these chapters provides a gradual, step-by-step tutorial for using classes and OOP in Python. It’s based upon a live demonstration I have been using in recent years in the training classes I teach, but has been honed here for use in a book. The chapter is designed to show OOP in a more realistic context than earlier examples and to illustrate how class concepts come together into larger, working programs. I hope it works as well here as it has in live classes.
这些章节首先提供一个循序渐进的指南,来讲述Python如何使用类和面向对象编程。这些指南基于我这些年在培训课上使用的现场演示,但是入书时已经经过了磨砺。这些章节被设计为以更现实的文字来展现面向对象编程,来描述类的概念如何组成一个更大的、运行中的程序。我希望这些指南能够像在现场授课一样奏效。
The last four of these new chapters are collected in a new final part of the book, “Advanced Topics.” Although these are technically core language topics, not every Python programmer needs to delve into the details of Unicode text or metaclasses. Because of this, these four chapters have been separated out into this new part, and are officially optional reading. The details of Unicode and binary data strings, for example, have been moved to this final part because most programmers use simple ASCII strings and don’t need to know about these topics. Similarly, decorators and metaclasses are specialist topics that are usually of more interest to API builders than application programmers.
后面四个章节被收录在本书最后新添加的大部分中,即“高级主题”,虽然这些内容理论上其实属于核心编程的主题,但是并不是每一个Python程序员都需要深入了解Unicode和原类。正因为这个原因,这4个章节被划分到新的部分,以供选读。比如说,相对于Unicode和二进制字符串处理,其实大多数程序员都仅仅使用ASCII字符串,所以既然大多数人都不理会这些主题我们就把它移到了新的部分。同样,装饰器和原类这些特殊主题,或许API制作者会比应用开发者更感兴趣。
If you do use such tools, though, or use code that does, these new advanced topic chapters should help you master the basics. In addition, these chapters’ examples include case studies that tie core language concepts together, and they are more substantial than those in most of the rest of the book. Because this new part is optional reading, it has end-of-chapter quizzes but no end-of-part exercises.
如果你使用这些工具,或者你使用的代码中提及了这些,那么这些新的主题将会帮助你掌握基础。此外,这些章节的例子配合核心编程思想提供了案例研究,这样的案例研究会比书中剩下那些章节中提到的要多。因为这些章节是选读的,所以将只提供了章节测试。
已有材料的改变
In addition, some material from the prior edition has been reorganized, or supplemented with new examples. Multiple inheritance, for instance, gets a new case study example that lists class trees in Chapter 30; new examples for generators that manually implement map and zip are provided in Chapter 20; static and class methods are illustrated by new code in Chapter 31; package relative imports are captured in action in Chapter 23; and the __contains__, __bool__, and __index__ operator overloading methods are illustrated by example now as well in Chapter 29, along with the new overloading protocols for slicing and comparison.
此外,为了支持新的实例,旧版本的材料已经被重做了。比如说,30章中的多重继承有了一个新的学习案例,这个案例演示了一个树类;在20章中提到了一个手动实现map和zip的生成器;在31章中,静态和类方法将使用新的编码方式展现;23章中展现了包相对依赖;__contains__、__bool__以及__index__这些操作符的重载方法将会在29章通过例子来描述,这个方法和新的切片和比较的重载协议有关。
This edition also incorporates some reorganization for clarity. For instance, to accommodate new material and topics, and to avoid chapter topic overload, five prior chapters have been split into two each here. The result is new standalone chapters on operator overloading, scopes and arguments, exception statement details, and comprehension and iteration topics. Some reordering has been done within the existing chapters as well, to improve topic flow.
这个版本出于清晰起见,对章节进行了重新组织。比如说为了适应新的材料和主题的加入,防止章节主题过载,过去的5个章节分别被分割为2个部分。这个结果导致了新的独立的章节的产生,涉及运算符重载、scope和arguments、异常状态申明、以及comprehension和迭代主题(译者注:部分不确定术语使用原文)。有些改变发生在已经存在的章节来提高主题的流畅性。
This edition also tries to minimize forward references with some reordering, though Python 3.0’s changes make this impossible in some cases: to understand printing and the string format method, you now must know keyword arguments for functions; to understand dictionary key lists and key tests, you must now know iteration; to use exec to run code, you need to be able to use file objects; and so on. A linear reading still probably makes the most sense, but some topics may require nonlinear jumps and random lookups.
这个版本同时尝试通过重新组织减少向前引用,尽管依据Python3.0的特性在一些代码中这是很难做到的:比如你必须了解方法的keyword参数才能理解打印和字符串格式化;你必须了解迭代器才能理解字典的key列表和测试;你需要能够使用文件对象,才能使用exec来运行代码等等。一个线性的阅读顺序是非常有意义的,但是有些主题可能需要非线性的跳跃和查找。
All told, there have been hundreds of changes in this edition. The next section’s tables alone document 27 additions and 57 changes in Python. In fact, it’s fair to say that this edition is somewhat more advanced, because Python is somewhat more advanced. As for Python 3.0 itself, though, you’re probably better off discovering most of this book’s changes for yourself, rather than reading about them further in this Preface.
总的来说,在这个版本中会有上百个改变。下一节的表格将展示Python3.0的27个新的增加和57个改变。事实上,可以说这个版本更加进一步,因为Python3.0也更进一步了。你最好自己去探索这本书中的改变,而不是这么早就在前言中读到他们。
2.6和3.0中的具体语言扩展
In general, Python 3.0 is a cleaner language, but it is also in some ways a more sophisticated language. In fact, some of its changes seem to assume you must already know Python in order to learn Python! The prior section outlined some of the more prominent circular knowledge dependencies in 3.0; as a random example, the rationale for wrapping dictionary views in a list call is incredibly subtle and requires substantial foreknowledge. Besides teaching Python fundamentals, this book serves to help bridge this knowledge gap.
一般来说,Python3.0比以往的更加清晰,但是在某些方面他也显得更加复杂。事实上,部分变化甚至需要你事先已经了解Python才能学习!前一个章节我们叙述了一些比较重要的3.0需要的背景知识;比如说,在列表调用中使用字典视图包装的理由是非常微妙,这需要相当的经验知识。除了教授Python的基础,这本书也旨在缩小这些知识的差距。
Table P-1 lists the most prominent new language features covered in this edition, along with the primary chapters in which they appear.
表P-1列出了这个版本所有重要的语言特性,同时也指出了这些特性出现的主要章节。(译者注:对于不了解的特性没有翻译。)
| 扩展内容 | 覆盖章节 |
|---|---|
| 3.0中的print函数 | 11 |
| 3.0中的外部变量x,y声明 | 17 |
| 2.6和3.0中的str.format方法 | 7 |
| 3.0中的字符串类型:
对于Unicode是str,对于二进制数据是bytes |
7、36 |
| 文本和二进制文件在3.0中的区别 | 9、36 |
| 2.6和3.0中的类装饰器:
@private(‘age’) |
31、38 |
| 3.0中的新迭代器:
range, map, zip |
14、20 |
| 3.0中的字典视图(Dictionary views):
D.keys, D.values, D.items |
8、14 |
| 3.0中的除法运算符:
remainders, / 和 // |
5 |
| 3.0中的集合(set)写法:
{a, b, c} |
5 |
| 3.0中的集合解析(set comprehensions):
{x**2 for x in seq} |
4、5、14、20 |
| 3.0中的字典解析(Dictionary comprehensions):
{x: x**2 for x in seq} |
4、8、 14、20 |
| 二进制数字字符串在2.6和3.0中的支持:
0b0101, bin(I) |
5 |
| 2.6和3中的分数数字类型:
Fraction(1, 3) |
5 |
| 3.0中的函数注解(Function annotations):
def f(a:99, b:str)->int |
19 |
| 3.0中参数arg只能以关键字参数(keyword-only)形式:
def f(a, *b, c, **d) |
18、20 |
| 3.0中扩展的序列拆包(sequence unpacking):
a, *b = seq |
11、13 |
| 包相对引入语法在3.0中有效:
from . |
23 |
| 2.6和3.0中Context管理器有效:
with/as |
33、35 |
| 3.0中异常处理语法改变:
raise,except/as,superclass |
33、34 |
| 3.0中的异常链(Exception chaining):
raise e2 from e1 |
33 |
| 2.6和3.0中保留字的变化 | 11 |
| New-style class cutover in 3.0 | 31 |
| 2.6和3.0中的属性装饰器(property decorators):
@property |
37 |
| Descriptor use in 2.6 and 3.0 | 31、38 |
| 2.6和3.0中的元类(Metaclass)使用 | 31、39 |
| 抽象基类在2.6和3.0中的支持 | 28 |