1. Introduction介绍¶
This reference manual describes the Python programming language. 本参考手册介绍Python编程语言。It is not intended as a tutorial.它不是一个教程。
While I am trying to be as precise as possible, I chose to use English rather than formal specifications for everything except syntax and lexical analysis. 虽然我试图尽可能精确,但除了语法和词法分析之外,我选择使用英语而不是正式规范。This should make the document more understandable to the average reader, but will leave room for ambiguities. 这应该会让普通读者更容易理解文档,但会留下模棱两可的余地。Consequently, if you were coming from Mars and tried to re-implement Python from this document alone, you might have to guess things and in fact you would probably end up implementing quite a different language. 因此,如果您来自Mars,并试图仅从本文档中重新实现Python,那么您可能不得不猜测一些事情,事实上,您可能最终实现了一种完全不同的语言。On the other hand, if you are using Python and wonder what the precise rules about a particular area of the language are, you should definitely be able to find them here. 另一方面,如果您正在使用Python,并且想知道该语言特定领域的精确规则是什么,那么您应该可以在这里找到它们。If you would like to see a more formal definition of the language, maybe you could volunteer your time — or invent a cloning machine :-).如果你想看到一个更正式的语言定义,也许你可以自愿花时间——或者发明一台克隆机:-)。
It is dangerous to add too many implementation details to a language reference document — the implementation may change, and other implementations of the same language may work differently. 在语言参考文档中添加太多的实现细节是很危险的,因为实现可能会发生变化,而相同语言的其他实现可能会有不同的工作方式。On the other hand, CPython is the one Python implementation in widespread use (although alternate implementations continue to gain support), and its particular quirks are sometimes worth being mentioned, especially where the implementation imposes additional limitations. 另一方面,CPython是一个被广泛使用的Python实现(尽管替代实现继续获得支持),其特殊的怪癖有时值得一提,尤其是在实现施加额外限制的情况下。Therefore, you’ll find short “implementation notes” sprinkled throughout the text.因此,您将在全文中找到简短的“实现说明”。
Every Python implementation comes with a number of built-in and standard modules. 每个Python实现都附带许多内置和标准模块。These are documented in The Python Standard Library. 这些都记录在Python标准库中。A few built-in modules are mentioned when they interact in a significant way with the language definition.在与语言定义进行重要交互时,会提到一些内置模块。
1.1. Alternate Implementations备选实施方案¶
Though there is one Python implementation which is by far the most popular, there are some alternate implementations which are of particular interest to different audiences.虽然有一个Python实现是目前为止最流行的,但有一些替代实现对不同的受众特别感兴趣。
Known implementations include:已知的实现包括:
- CPython
This is the original and most-maintained implementation of Python, written in C. New language features generally appear here first.这是用C编写的Python的原始实现,也是维护最多的实现。新的语言特性通常首先出现在这里。- Jython
Python implemented in Java. This implementation can be used as a scripting language for Java applications, or can be used to create applications using the Java class libraries.用Java实现的Python。此实现可以用作Java应用程序的脚本语言,也可以用于使用Java类库创建应用程序。It is also often used to create tests for Java libraries.它还经常用于为Java库创建测试。More information can be found at the Jython website.更多信息可以在Jython网站上找到。- Python for .NET
This implementation actually uses the CPython implementation, but is a managed .NET application and makes .NET libraries available.此实现实际上使用了CPython实现,但它是一个托管的.NET应用程序,并使.NET库可用。It was created by Brian Lloyd.它是由布莱恩·劳埃德创建的。For more information, see the Python for .NET home page.有关更多信息,请参阅Python For .NET主页。- IronPython
An alternate Python for .NET. Unlike Python.NET, this is a complete Python implementation that generates IL, and compiles Python code directly to .NET assemblies..NET的替代Python。与Python.NET不同,这是一个完整的Python实现,它生成IL,并将Python代码直接编译到网络程序集。It was created by Jim Hugunin, the original creator of Jython.它是由吉姆·胡古宁(Jim Hugunin)创建的,他是Jython的原始创造者。For more information, see the IronPython website.有关更多信息,请参阅IronPython网站。- PyPy
An implementation of Python written completely in Python.完全用Python编写的Python实现。It supports several advanced features not found in other implementations like stackless support and a Just in Time compiler.它支持一些在其他实现中找不到的高级特性,如无堆栈支持和实时编译器。One of the goals of the project is to encourage experimentation with the language itself by making it easier to modify the interpreter (since it is written in Python).该项目的目标之一是通过更容易修改解释器(因为它是用Python编写的),鼓励对语言本身进行实验。Additional information is available on the PyPy project’s home page.PyPy项目的主页上提供了其他信息。
Each of these implementations varies in some way from the language as documented in this manual, or introduces specific information beyond what’s covered in the standard Python documentation. 这些实现中的每一个都在某种程度上不同于本手册中记录的语言,或者引入了标准Python文档中所涵盖的特定信息。Please refer to the implementation-specific documentation to determine what else you need to know about the specific implementation you’re using.请参阅特定于实现的文档,以确定您还需要了解哪些有关您正在使用的特定实现的信息。
1.2. Notation记号法¶
The descriptions of lexical analysis and syntax use a modified BNF grammar notation. 词法分析和语法的描述使用了修改后的BNF语法记号法。This uses the following style of definition:这使用以下定义样式:
name ::=lc_letter
(lc_letter
| "_")*
lc_letter ::= "a"..."z"
The first line says that a 第一行表示name
is an lc_letter
followed by a sequence of zero or more lc_letter
s and underscores. name
是一个lc_letter
,后跟一系列零个或多个lc_letter
和下划线。An lc_letter
in turn is any of the single characters 'a'
through 'z'
. lc_letter
依次是从'a'
到'z'
的任何单个字符。(This rule is actually adhered to for the names defined in lexical and grammar rules in this document.)(本文档中词汇和语法规则中定义的名称实际上遵守此规则。)
Each rule begins with a name (which is the name defined by the rule) and 每个规则都以一个名称(即规则定义的名称)和::=
. ::=
开头。A vertical bar (竖条(|
) is used to separate alternatives; it is the least binding operator in this notation. |
)用于分隔备选方案;它是此表示法中最小的绑定运算符。A star (星号(*
) means zero or more repetitions of the preceding item; likewise, a plus (+
) means one or more repetitions, and a phrase enclosed in square brackets ([ ]
) means zero or one occurrences (in other words, the enclosed phrase is optional). *
)表示前一项的零次或多次重复;同样,加号(+
)表示一次或多次重复,方括号([]
)中的短语表示零次或一次出现(换句话说,所附短语是可选的)。The 文字字符串用引号括起来。*
and +
operators bind as tightly as possible; parentheses are used for grouping. Literal strings are enclosed in quotes. *
和+
运算符尽可能紧密地绑定;括号用于分组。文字字符串用引号括起来。White space is only meaningful to separate tokens. 空白仅对分隔标记有意义。Rules are normally contained on a single line; rules with many alternatives may be formatted alternatively with each line after the first beginning with a vertical bar.规则通常包含在一行中;具有多个备选方案的规则可以在第一行以竖条开始后的每一行交替格式化。
In lexical definitions (as the example above), two more conventions are used: Two literal characters separated by three dots mean a choice of any single character in the given (inclusive) range of ASCII characters. 在词汇定义中(如上例),使用了另外两种约定:由三个点分隔的两个文字字符表示在给定(包括)ASCII字符范围内选择任何单个字符。A phrase between angular brackets (尖括号(<...>
) gives an informal description of the symbol defined; e.g., this could be used to describe the notion of ‘control character’ if needed.<<...>
)之间的短语给出了所定义符号的非正式描述;例如,如果需要,这可以用来描述“控制字符”的概念</p>
Even though the notation used is almost the same, there is a big difference between the meaning of lexical and syntactic definitions: a lexical definition operates on the individual characters of the input source, while a syntax definition operates on the stream of tokens generated by the lexical analysis. 尽管使用的符号几乎相同,但词汇定义和语法定义的含义有很大区别:词汇定义作用于输入源的各个字符,而语法定义作用于词汇分析生成的标记流。All uses of BNF in the next chapter (“Lexical Analysis”) are lexical definitions; uses in subsequent chapters are syntactic definitions.在下一章(“词汇分析”)中,BNF的所有用法都是词汇定义;在接下来的章节中使用的是语法定义。