学习python 第四版
七/100
其实已经不是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也已经发布了。