Differences between revisions 3 and 4
Revision 3 as of 2007-01-02 07:21:24
Size: 9084
Editor: HuangYi
Comment:
Revision 4 as of 2007-01-02 07:29:55
Size: 9078
Editor: HuangYi
Comment:
Deletions are marked like this. Additions are marked like this.
Line 17: Line 17:
就像序言中说过的那样,本书针对的读者会微不太一样。 就像序言中说过的那样,本书针对的读者会微不太一样。
Line 32: Line 32:
本附录将会简要地谈谈 python 语言本身的每一个重要组成,但不会涉及任何库
 (甚至是标准库和在本书主要章节中讨论的常用库)。也不会涉及到所有语法和语义的细节。
不过,这篇回顾应该足让读者理解本书中所有例子了。
本附录将会简要地谈谈 python 语言本身的每一个重要组成部分,但不会涉及任何库
 (甚至是标准库和在本书主要章节中讨论的常用库)。也不会涉及到所有语法和语义的细节。
不过,这篇回顾应该足让读者理解本书中所有例子了。
Line 49: Line 49:
甚至对 python 很熟悉的读者可能会喜欢这篇回顾。
这篇摘要的目标 spin 和大部分介绍都不一样。
我相信我这种对语言特色分类和解释的方式能为你提供一种新鲜的——同时也是准确的——角度来看待 python 语言。
甚至对 python 很熟悉的读者可能会喜欢这篇回顾。
这篇摘要的目标 spin 和大部分介绍文章都不一样。
我相信我这种对语言特色分类和解释的方式能为你提供一种新鲜的——也是同样准确的——看待 python 语言的角度
Line 53: Line 53:
这篇附录不会特意回避一些计算机科学里面抽象术语——如果你对某一术语不熟悉,你大可直接跳过那一段,不会有什么损失的; 这篇附录不会特意回避一些计算机科学里面抽象术语——如果你对某一术语不熟悉,你大可直接跳过那一段,不会有什么损失的;
Line 73: Line 73:
在大部分情况下,一个被编译成字节码的应用程序会被缓存起来,这样在以后运行的时候可以提高性能
不过不管在什么地方进行的编译过程都是在“幕后”悄悄完成的。
在大部分情况下,一个被编译成字节码的应用程序会被缓存起来,这样在以后运行的时候可以加快速度
不过不管在什么地方进行的必要的编译过程都是在“幕后”悄悄完成的。
Line 94: Line 94:
它依赖于约定来进行访问控制和控制名字的可见性,而不是通过强制机制。
不过不考虑它的面向对象的核心,python 的大部分语都设计成方便的面向过程式 (procedural) 的风格,
它依赖于约定而非强制机制来进行访问控制和名字的可见性制。
撇开它的面向对象的核心,python 的大部分语都设计成方便的面向过程式 (procedural) 的风格,

TableOfContents

附录 —— 有选择性的让人印象深刻的 python 简短回顾

APPENDIX -- A Selective and Impressionistic Short Review of Python


  • A reader who is coming to Python for the first time would be well served reading Guido van Rossum's _Python Tutorial_, which can be

    downloaded from <http://python.org/>, or picking up one of the several excellent books devoted to teaching Python to novices. As indicated in the Preface, the audience of this book is a bit different.

第一次接触 python 的读者适合去读 GUIdo van Rossum 的 Python Tutorial, 可以从 http://python.org/ 下载, 或者选一本针对 python 初学者的好书。 就像序言中说过的那样,本书针对的读者会稍微不太一样。

  • The above said, some readers of this book might use Python only infrequently, or not have used Python for a while, or may be sufficiently versed in numerous other programming languages, that a quick review on Python constructs suffices for understanding. This appendix will briefly mention each major element of the Python language itself, but will not address any libraries (even standard and ubiquitous ones that may be discussed in the main chapters). Not all fine points of syntax and semantics will be covered here, either. This review, however, should suffice for a reader to understand all the examples in this book.

上面说了,本书的读者也许只是不常使用 python,或是有一段时间没有过 python , 或是精通许多其它语言,对他们来说只要对 python 来一点简短的回顾他们就能懂了。 本附录将会简要地谈谈 python 语言本身的每一个重要组成部分,但不会涉及任何库

  • (甚至是标准库和在本书主要章节中讨论的常用库)。也不会涉及到所有语法和语义的细节。

不过,这篇回顾应该足够让读者理解本书中所有例子了。

  • Even readers who are familiar with Python might enjoy skimming this review. The focus and spin of this summary are a bit different from most introductions. I believe that the way I categorize and explain a number of language features can provide a moderately novel--but equally accurate--perspective on the Python language. Ideally, a Python programmer will come away from this review with a few new insights on the familiar constructs she uses every day. This appendix does not shy away from using some abstract terms from computer science--if a particular term is not familiar to you, you will not lose much by skipping over the sentence it occurs in; some of these terms are explained briefly in the Glossary.

甚至对 python 很熟悉的读者可能都会喜欢这篇回顾。 这篇摘要的目标及 spin 和大部分介绍文章都不一样。 我相信我这种对语言特色分类和解释的方式能为你提供一种新鲜的——也是同样准确的——看待 python 语言的角度。 理想的情况下,python 程序员看完这篇回顾后,应该会让他对自己经常使用的熟知的东西(constructs)有一些新的看法。 这篇附录不会特意回避一些计算机科学里面的抽象术语——如果你对某一术语不熟悉,你大可直接跳过那一段,不会有什么损失的; 某些术语在术语表中有简短的解释。

第一节 —— python 属于哪种类型的语言

SECTION -- What Kind of Language is Python?


  • Python is a byte-code compiled programming language that supports multiple programming paradigms. Python is sometimes called an interpreted and/or scripting language because no separate compilation step is required to run a Python program; in more precise terms, Python uses a virtual machine (much like Java or Smalltalk) to run machine-abstracted instructions. In most situations a byte-code compiled version of an application is cached to speed future runs, but wherever necessary compilation is performed "behind the scenes."

python 是一个字节码编译型的语言,它支持多种编程范式。 由于运行一个 python 程序并不需要单独的编译步骤,所以有时候 python 也被叫做是解释型的 和/或 脚本语言; 用更精确的术语来说,python 使用一个虚拟机 (很像 Java 或是 Smalltalk) 来运行抽象机器的指令 (machine-abstracted instructions)。 在大部分情况下,一个被编译成字节码的应用程序会被缓存起来,这样在以后运行的时候可以加快速度, 不过不管在什么地方进行的必要的编译过程都是在“幕后”悄悄完成的。

  • In the broadest terms, Python is an imperative programming language, rather than a declarative (functional or logical) one. Python is dynamically and strongly typed, with very late binding compared to most languages. In addition, Python is an object-oriented language with strong introspective facilities, and one that generally relies on conventions rather than enforcement mechanisms to control access and visibility of names. Despite its object-oriented core, much of the syntax of Python is designed to allow a convenient procedural style that masks the underlying OOP mechanisms. Although Python allows basic functional programming (FP) techniques, side effects are the norm, evaluation is always strict, and no compiler optimization is performed for tail recursion (nor on almost any other construct).

用最宽泛的术语来说,python 是一种命令式 (imperative) 的编程语言,而非声明式 (函数式或逻辑式) 的。 python 是动态类型且是强类型的语言,相对大部分语言来说它拥有真正的迟绑定。 另外 python 还是一个拥有强大内省 (introspective) 机制的面向对象语言, 它依赖于约定而非强制机制来进行访问控制和名字的可见性控制。 撇开它的面向对象的核心,python 的大部分语法都设计成方便的面向过程式 (procedural) 的风格, 通过它来 (mask) 底层的面向对象机制。 虽然 python 允许基本的函数式编程 (FP) 技术,不过边界效应 (side effects) 还是正常的 (norm), 求值也总是严格的,而且还不会对尾递归(还有几乎所有其它的东西)进行编译器优化。

  • Python has a small set of reserved words, delimits blocks and structure based on indentation only, has a fairly rich collection of built-in data structures, and is generally both terse and readable compared to other programming languages. Much of the strength of Python lies in its standard library and in a flexible system of importable modules and packages.

python 有一个不大的保留字集合,分界块 (delimits blocks) 和仅基于缩进的层次结构, 还拥有一组相当丰富的内置数据结构,而且相对其它语言来说很简洁,可读性也很强。 python 很多强大能力存在于它的标准库和灵活的模块/包系统之中。

第二节 —— 名字空间与绑定

SECTION -- Namespaces and Bindings


  • The central concept in Python programming is that of a namespace. Each context (i.e., scope) in a Python program has available to it a hierarchically organized collection of namespaces; each namespace contains a set of names, and each name is bound to an object. In older versions of Python, namespaces were arranged according to the "three-scope rule" (builtin/global/local), but Python version 2.1 and later add lexically nested scoping. In most cases you do not need to worry about this subtlety, and scoping works the way you would expect (the special cases that prompted the addition of lexical scoping are mostly ones with nested functions and/or classes).

使用 python 编程的核心概念就是名字空间。 python 程序中的每一个上下文 (或者说是作用范围) 都拥有一组层次结构的名字空间; 每一个名字空间包含一组名字,每一个名字绑定到一个对象。 在老版本的 python 中,使用 “三层范围规则” (内置/全局/局部) 对名字空间进行组织, 不过 python 2.1 及其后的版本都增加了嵌套的作用范围。 在大部分情况下你并不需要考虑这种微妙的东西,而且作用范围工作的方式和你所期待的是一样的。 (需要增加另外的 lexical scoping 的特例大部分都是嵌套函数 和/或 嵌套类)

  • There are quite a few ways of binding a name to an object within the current namespace/scope and/or within some other scope. These various ways are listed below.

有好几种方法可以在当前的名字空间/作用范围 和/或 一些其它的作用范围中将一个名字绑定到对象上去。 这些方法有:

TPiP/AppendixA (last edited 2009-12-25 07:17:07 by localhost)