学习Python第四版(四)

14
八/10
1

这一篇文章主要针对前言剩下部分内容中第四版与第三版区别的修订。主要将以表格方式呈现。下一篇学习Python的日志将正式开始正文。(译者注:以后所有的修订类的翻译都讲按本文的格式进行。)

翻译者:Banumgoogle

前言(续)

与3rd中区别的修订。
前言修订部分
在3版中文的页码 原文 修订
前言1 新的B if A else C条件表达式(第12章) 新的B if A else C条件表达式(第19章)
前言1 with/as环境管理器(第27章) with/as环境管理器(第33章)
前言2 统一try/except/fanilly(第27章) 统一try/except/fanilly(第33章)
前言2 相对导入语法(第21章) 相对导入语法(第23章)
前言2 生成器表达式(第17章) 生成器表达式(第20章)
前言2 新的生成器函数特性(第17章) 新的生成器函数特性(第20章)
前言2 函数装饰器(第26章) 生成器表达式(第31章)
前言2 新的生成器函数特性(第17章) 新的生成器函数特性(第20章)
前言2 新的内置函数:sort、sum、any、all、
enumerate(第4章和第13章)
新的内置函数:sort、sum、any、all、
enumerate(第13章和第14章)
前言2 新的生成器函数特性(第17章) 新的生成器函数特性(第20章)
前言2 文件、列表解析和迭代器等新的扩展内容(第13章和第17章) 文件、列表解析和迭代器(第14章和第20章)
前言2 新的开发工具:Eclipse、distutils、unittest和doctest、IDLE加强版、Shedskin等(第3章和第29章) 新的开发工具:Eclipse、distutils、unittest和doctest、IDLE加强版、Shedskin等(第2章和第35章)
前言2 详细的OOP介绍(第22章) 详细的OOP介绍(第25章)
前言3 这种新结构….读者产生恐惧 这种新结构….读者产生恐惧
前言3 练习题解答在附录B 章节后的习题将直接出现在习题的最后,这和练习题不同,后者的答案在附录B
前言3 在适当的地方,…掩盖了基础知识 在适当的地方,…掩盖了基础知识
前言4 其他书籍…《Python Pocket Reference》 其他书籍
前言4 因为本书的侧重点…练习题(参考第29章) 因为本书的侧重点…练习题(参考第29章)
前言6 习题和练习题的解答在附录B 习题的解答在每章的最后,而练习题的答案则在附录B
前言6 附录B的解答应该可以帮助你 章节后和附录B中的解答应该可以帮助你
前言7 第8部分 新的第8部分,原附录成为第9部分。新添加部分将在表后附一中给出
前言8 关于本书的程序 第一段文字被重写了。在附二中给出
前言8 迎接Python3.0 此部分被删
附一

Part VIII, Advanced Topics (new in the fourth edition)

第8部分,高级主题(4版中全新)

In the final part, we explore some advanced topics. Here, we study Unicode and byte strings, managed attribute tools like properties and descriptors, function and class decorators, and metaclasses. These chapters are all optional reading, because not all programmers need to understand the subjects they address. On the other hand, readers who must process internationalized text or binary data, or are responsible for developing APIs for other programmers to use, should find something of interest in this part.

在最后一个部分,我们将探索一些高级的主题,在这一部分,我们将学习Unicode和Byte两种类型的字符串,管理特性工具(如properties和descriptors,函数和类装饰器,以及原类)。这些章节都是选读的,应为不是所有的程序员都需要了解这些内容。另一方面,对于那些需要处理国际化和二进制数据,或者负责为其他程序员开发api的读者,这个部分或许是他们感兴趣的。

附录二

This fourth edition of this book, and all the program examples in it, is based on Python version 3.0. In addition, most of its examples run under Python 2.6, as described in the text, and notes for Python 2.6 readers are mixed in along the way.

本书的第四版,以及所有其中提及的例子,都是基于Python3.0的。此外,大多数运行在Python2.6下的例子和针对Python2.6读者的笔记也将同时被提及。

译者的体例

读过Oreilly书的人都了解它的脚印(tips)和陷阱(warning)。为了能够在我的翻译中也表现出这些特点,我将使用斜体表示tips,用红色字体来表示warning。

Tagged as:

学习Python第四版(三)

13
八/10
0

即上一次更新隔了1个多礼拜。公司里项目跟进一时就没了时间,所以只好暂缓两天。今天将继续前言部分,并给前言一个结尾。还要给出一个说明,因为learning python的第三版的中文版已有成书,在这里获得,所以四版和三版的重复内容我将略过内容,不予以翻译,。但是如果有修订和更改,我将会特别得指出,并指明在3版中文版的页码,对应于4版原版的位置。

翻译者:Banumgoogle

前言(续)

3.0中被移除的部分

In addition to extensions, a number of language tools have been removed in 3.0 in an effort to clean up its design. Table P-2 summarizes the changes that impact this book, covered in various chapters of this edition. Many of the removals listed in Table P-2 have direct replacements, some of which are also available in 2.6 to support future migration to 3.0.

除了上文提到的扩展,有一些语言工具在3.0中被移除了,这样做是为了使架构更加清晰。这些改变将在表P-2中叙述,他们将覆盖这个版本的若干章节。有一些被移除的特性被直接替换了,还有一部分其实在2.6中已经提供了支持以方便未来向3.0迁移。

表P-2. 这本书涉及的3.0中的移除内容
移除内容 替换内容 覆盖的章节
reload(M) imp.reload(M) (or exec) 3, 22
apply(f, ps, ks) f(*ps, **ks) 18
`X` repr(X) 5
X <> Y X != Y 5
long int 5
9999L 9999 5
D.has_key(K) K in D (或者 D.get(key) != None) 8
raw_input input 3, 10
old input eval(input()) 3
xrange range 14
file open (以及 io 模型类库) 9
X.next X.__next__, 通过next(X)调用 14, 20, 29
X.__getslice__ X.__getitem__ 接受一个slice对象 7, 29
X.__setslice__ X.__setitem__ 接受一个slice对象 7, 29
reduce functools.reduce (或者使用循环语句) 14, 19
execfile(filename) exec(open(filename).read()) 3
exec open(filename) exec(open(filename).read()) 3
0777 0o777 5
print x, y print(x, y) 11
print >> F, x, y print(x, y, file=F) 11
print x, y, print(x, y, end=’ ‘) 11
u’ccc’ ‘ccc’ 7, 36
‘bbb’ for byte strings b’bbb’ 7, 9, 36
raise E, V raise E(V) 32, 33, 34
except E, X: except E as X: 32, 33, 34
def f((a, b)): def f(x): (a, b) = x 11, 18, 20
file.xreadlines for line in file: (或者X=iter(file)) 13, 14
D.keys()之类作为lists返回 list(D.keys()) (字典视图) 8, 14
map(), range(), 等作为lists返回 list(map()), list(range()) (内置) 14
map(None, …) zip (或者手动编码来 pad 结果) 13, 20
X=D.keys(); X.sort() sorted(D) (或者list(D.keys())) 4, 8, 14
cmp(x, y) (x > y) – (x < y) 29
X.__cmp__(y) __lt__, __gt__, __eq__,等 29
X.__nonzero__ X.__bool__ 29
X.__hex__, X.__oct__ X._index__ 29
Sort comparison functions 使用 key=transform 或 reverse=True 8
Dictionary <, >, <=, >= Compare sorted(D.items()) (或者使用循环) 8, 9
types.ListType list (types只针对于nonbuilt-in names) 9
__metaclass__ = M class C(metaclass=M): 28, 31, 39
__builtin__ builtins (改名) 17
Tkinter tkinter (改名) 18, 19, 24, 29, 30
sys.exc_type, exc_value sys.exc_info()[0], [1] 34, 35
function.func_code function.__code__ 19, 38
__getattr__ 以内置方式运行 在wrapper类中重新定义了__X__方法 30, 37, 38
-t, –tt 命令行替换 不正确的tab和空间使用总是返回一个错误 10, 12
from … *, within a function 可能只在文件的高层出现 22
import mod, in same package from . import mod, package-relative form 23
class MyException: class MyException(Exception): 34
异常模块(exceptions module) Built-in scope, library manual 34
thread, Queue modules _thread, queue (都被重命名了) 17
cPickle module _pickle (重命名了, 自动使用) 9
os.popen2/3/4 subprocess.Popen (os.popen保留) 14
基于字符串的错误 基于类的错误 (这点在2.6中已经被要求) 32, 33, 34
字符模块函数 字符对象方法 7
未绑定的方法 Functions(通过instance来调用静态方法) 30, 31
混合类型的比较和排序 非数字的混合类型比较会抛出错误 5, 9

There are additional changes in Python 3.0 that are not listed in this table, simply because they don’t affect this book. Changes in the standard library, for instance, might have a larger impact on applications-focused books like Programming Python than they do here; although most standard library functionality is still present, Python 3.0 takes further liberties with renaming modules, grouping them into packages, and so on. For a more comprehensive list of changes in 3.0, see the “What’s New in Python 3.0” document in Python’s standard manual set.

Python3.0中的其他一些变化没有在表中列出,因为它们对我们这本书没有影响。比如说,标准库中的一些变化,可能对面向于应用开发的书影响比较大,虽然大多数的标准库依然运作良好,Python3.0将在重命名、打包模块等方面拥有更多的自由。如果你想获取3.0中改变的进一步信息,请参阅Python标准手册中的”What’s New in Python 3.0″文档。

If you are migrating from Python 2.X to Python 3.X, be sure to also see the 2to3 automatic code conversion script that is available with Python 3.0. It can’t translate everything, but it does a reasonable job of converting the majority of 2.X code to run under 3.X. As I write this, a new 3to2 back-conversion project is also underway to translate Python 3.X code to run in 2.X environments. Either tool may prove useful if you must maintain code for both Python lines; see the Web for details.

如果你正从2.X版本迁移到3.X版本,请确保看看2to3自动代码转化脚本,这个脚本在Python3.0下可用。它不能翻译所有一切,但是可以让2.X中大多数的代码运行在3.X下。我写这本书的时候,新的3to2转化项目也在进行之中,目的在于转化3.X代码使它们能够运行在2.6环境下。两个工具也许能够在你必须维护两套Python版本时给与帮助。

Because this fourth edition is mostly a fairly straightforward update for 3.0 with a handful of new chapters, and because it’s only been two years since the prior edition was published, the rest of this Preface is taken from the prior edition with only minor updating.

因为第四版主要是对于3.0的更新提供新的少量章节,同时也因为离上一版出版才两年的时间,前言的剩下部分将从前一个版本中直接拿过来,只有极少量的更新。(译者注:剩下部分请参阅3版中文)

Tagged as:

学习Python第四版(二)

2
八/10
0

继续上次的工作,今天的翻译内容主要涉及4版和3版的新增章节。并且将会提及2.6和3.0中的改变和新特性。

翻译者:Banumgoogle

前言(续)

新添加的章节

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列出了这个版本所有重要的语言特性,同时也指出了这些特性出现的主要章节。(译者注:对于不了解的特性没有翻译。)

表P-1。Python2.6和3.0的语言扩展
扩展内容 覆盖章节
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

学习python 第四版

31
七/10
0

其实已经不是Python的纯新手了,之前赶项目的时候用过python,由于时间比较急,所以就挑了一本不怎么样的书大致看了一下,本来想做完就扔掉的,没想到通过这个项目我却迷上了这个动人的语言,所以就找了这本饱受好评的learning python来看了。最新的第四版,暂无中文版,我就自行翻译了。英语未过6级,语文也不好,结果估计会比较悲剧,看官们请不要介意啊。

为了兼顾保留原文和阅读流畅性,我将en文部分设为了白色,如果需要查看请用鼠标选中。

原书名:Learning Python 4th Edition

作者:Mark Lutz

翻译者:Banum

前言

This book provides an introduction to the Python programming language. Python is a popular open source programming language used for both standalone programs and scripting applications in a wide variety of domains. It is free, portable, powerful, and remarkably easy and fun to use. Programmers from every corner of the software in-dustry have found Python’s focus on developer productivity and software quality to be a strategic advantage in projects both large and small.

这本书介绍了Python这种编程语言。 Python是一种在独立程序和脚本领域广泛使用的开源语言。它是免费、轻量、功能强大的,它非常容易使用同时充满了乐趣。来自软件业各个角落的程序员们发现无论项目大小,Python都能着眼于在软件生产率和软件质量两个方面,获得巨大的战略优势。

Whether you are new to programming or are a professional developer, this book’s goal is to bring you quickly up to speed on the fundamentals of the core Python language. After reading this book, you will know enough about Python to apply it in whatever application domains you choose to explore.

无论你是编程菜鸟还是专家,这本书的目标就是带你在Python核心编程基础上获得快速提升。读完这本书,你就能够对Python有一个足够的了解,使它适用于每一个你选择探索的应用领域。

By design, this book is a tutorial that focuses on the core Python language itself, rather than specific applications of it. As such, it’s intended to serve as the first in a two-volume set:

  • Learning Python, this book, teaches Python itself.
  • Programming Python, among others, shows what you can do with Python after you’ve learned it.

在设计时,这本书被定位为一本重点讲述核心Python编程本身的教程,而不是着眼于特定的应用领域。所以,这本书的目标是成为下面两类中的前者:

  • 《学习Python》,也就是这本书,教授Python本身。
  • 《Python编程》,其他那些中的一本,展现你学习了Python以后可以做什么。

That is, applications-focused books such as Programming Python pick up where this book leaves off, exploring Python’s role in common domains such as the Web, graphical user interfaces (GUIs), and databases. In addition, the book Python Pocket Reference provides additional reference materials not included here, and it is designed to supplement this book.

即,重点于应用的书(比如《Python编程》)应该在这本书读完后拿来继续研习,用以探索Python在Web、图形用户界面(GUI)以及数据库等常规领域的角色。此外,《Python袖珍参考书》这本书所提供的额外的参考材料本书也没有涉及,它被设计为对本书的另外一种支持。

Because of this book’s foundations focus, though, it is able to present Python fundamentals with more depth than many programmers see when first learning the language. And because it’s based upon a three-day Python training class with quizzes and exercises throughout, this book serves as a self-paced introduction to the language.

正因为这本书的基础目标(尽管第一次学习一种语言我们可以学得比很多程序员所看到得更深入),也因为这本书是在为其3天的带有练习和测试的Python教学课的难度基础上,这本书将会以适合自学的方式来介绍这种语言。

关于第四版

This fourth edition of this book has changed in three ways. This edition:

  • Covers both Python 3.0 and Python 2.6—it emphasizes 3.0, but notes differences in 2.6
  • Includes a set of new chapters mainly targeted at advanced core-language topics
  • Reorganizes some existing material and expands it with new examples for clarity

第四版在3个方面进行了改变:

  • 覆盖Python3.0以及Python2.6——强调3.0的特性,但会标注在2.6中的区别
  • 增加了一系列新的章节,这些章节主要针对于最新的核心编程主题
  • 整理了已存在的材料,同时为了更清晰的阐述扩充一些新的例子

As I write this edition in 2009, Python comes in two flavorversion 3.0 is an emerging and incompatible mutation of the language, and 2.6 retains backward compatibility with the vast body of existing Python code. Although Python 3 is viewed as the future of Python, Python 2 is still widely used and will be supported in parallel with Python 3 for years to come. While 3.0 is largely the same language, it runs almost no code written for prior releases (the mutation of print from statement to function alone, aesthetically sound as it may be, breaks nearly every Python program ever written).

当我在2009年开始写这个版本时,Python出现了两种版本,3.0是一种新出现的和以前的版本显得不兼容,而2.6则和现有的Python主体保持了兼容性。尽管Python3被视为是未来的Python,但是Python2依然被广泛地使用并且与Python3一起在接下来的几年获得支持。虽然3.0大致上还是那个Python,但是过去发布的Python大多是无法运行的(打印从申明变成了函数形式,虽然它可能变得更美观,但是这使得几乎所有的Python程序都被弄坏了。)

This split presents a bit of a dilemma for both programmers and book authors. While it would be easier for a book to pretend that Python 2 never existed and cover 3 only, this would not address the needs of the large Python user base that exists today. A vast amount of existing code was written for Python 2, and it won’t be going away any time soon. And while newcomers to the language can focus on Python 3, anyone who must use code written in the past needs to keep one foot in the Python 2 world today. Since it may be years before all third-party libraries and extensions are ported to Python 3, this fork might not be entirely temporary.

这种版本划分使得无论是程序员还是书的作者都很难办。如果假装Python2从来没有存在过,而只写3的特性,这会使成书工作显得比较容易,但是这样做却无法满足今天已经存在的巨大的Python使用者群体的需要。大量的程序都是用Python2写就,并且在短期内并不会更新或者遗弃。然而新的语言学习者就可以将重点放在Python3上,那些依然需要使用Python2的代码书写方式进行工作的人则必须时刻关注Python2的动向。因为第三方库和扩展完全支持python3尚需要一定的年限,所以这样的一种状况恐怕不会是暂时的。

涵盖Python2.6和3.0

To address this dichotomy and to meet the needs of all potential readers, this edition of this book has been updated to cover both Python 3.0 and Python 2.6 (and later releases in the 3.X and 2.X lines). It’s intended for programmers using Python 2, programmers using Python 3, and programmers stuck somewhere between the two.

注意到这两种版本的存在,为了满足所有潜在读者的需要,这版图书将升级为同时涵盖Python3.0和Python2.6。这样做即方便那些正在使用Python2的程序员,也考虑使用Python3的程序员,同时顾及那些两版本都需要的程序员。

That is, you can use this book to learn either Python line. Although the focus here is on 3.0 primarily, 2.6 differences and tools are also noted along the way for programmers using older code. While the two versions are largely the same, they diverge in some important ways, and I’ll point these out along the way.

也就是说你可以通过这本书学习任何一个版本的Python。虽然这本书的重点在于Python3.0,但是它和2.6的区别以及工具也会按照使用旧版本代码的程序员习惯提及。两个版本基本上是相同的,在重要的区别点上,我会给读者指出来。

For instance, I’ll use 3.0 print calls in most examples, but will describe the 2.6 print statement, too, so you can make sense of earlier code. I’ll also freely introduce new features, such as the nonlocal statement in 3.0 and the string format method in 2.6 and 3.0, and will point out when such extensions are not present in older Pythons.

举例来说,我会在大多数的示例中使用3.0的print方法,但也会对2.6的print声明进行描述,这样你就可以了解前一个版本的代码风格。我同时也会介绍一些新的功能,比如说全局变量在3.0中的声明以及2.6与3.0的字符串格式化方法,并且当一些扩展在早期版本的Python不存在时我也会告知大家。

If you are learning Python for the first time and don’t need to use any legacy code, I encourage you to begin with Python 3.0; it cleans up some longstanding warts in the language, while retaining all the original core ideas and adding some nice new tools.Many popular Python libraries and tools will likely be available for Python 3.0 by the time you read these words, especially given the file I/O performance improvements expected in the upcoming 3.1 release. If you are using a system based on Python 2.X, however, you’ll find that this book addresses your concerns, too, and will help you migrate to 3.0 in the future.

如果你是Python的新手,那么就不需要使用那些遗留代码了,强烈建议你从3.0开始;新版本清除了那些存在了很久的问题,同时保留了原始的核心思想,增加了许多新的工具。大多数流行的Python库在你读到这本书时也将支持3.0版本,特别是3.1版本中将要发布的文件I/O性能的提升,这些都让人欢欣鼓舞。如果你还在使用一个基于2.X版本的Python,没关系,这本书也值得一读,他将帮助你在未来迁移到3.0。

By proxy, this edition addresses other Python version 2 and 3 releases as well, though some older version 2.X code may not be able to run all the examples here. Although class decorators are available in both Python 2.6 and 3.0, for example, you cannot use them in an older Python 2.X that did not yet have this feature. See Tables P-1 and P-2 later in this Preface for summaries of 2.6 and 3.0 changes.

通过代理,这个版本提及了Python2和3的其他发行版本,尽管书中的例子大多无法再更久版本的2.X中运行。举例来说,Python2.6和3.0中的类装饰符,你在更老的2.X中使用它们,因为它们是新的特性。前言后面的表P-1和P-2将描述2.6和3.0中的这些变化。

Shortly before going to press, this book was also augmented with notes about prominent extensions in the upcoming Python 3.1 release–comma separators and automatic field numbering in string format method calls, multiple context manager syntax in with statements, new methods for numbers, and so on. Because Python 3.1 was targeted primarily at optimization, this book applies directly to this new release as well. In fact, because Python 3.1 supersedes 3.0, and because the latest Python is usually the best Python to fetch and use anyhow, in this book the term “Python 3.0” generally refers to the language variations introduced by Python 3.0 but that are present in the entire 3.X line.

就在出版的前不久,我们又根据即将到来的Python3.1的重要扩展通过注释对本书进行了加强——这包括字符串格式化方法调用中的逗号分割和自动区域编号,通过声明的多上下文管理器语法,新的数字方法等。因为Python3.1的目标在于优化,所以这本书也直接适用于新的版本。事实上,因为3.1会取代3.0,也因为最新的Python通常也是最适用获取和使用的Python,所以在这本书中,术语“Python3.0”通常是指3.0版本中所引入的语言变化,但同时也适用于整个3.X系列。就在译者翻译这篇文章的时候python3.2也已经发布了。