1. 介绍

此参考手册描述 Python 编程语言。不打算作为教程。

虽然试着尽可能准确,但除句法和词法分析外,我选择使用英语而不是正式规范。这应该能使普通读者更理解文档,但会为歧义留下空间。因此,若您来自火星并试着仅从此文档重实现 Python,可能不得不猜测事情,且事实上可能最终实现完全不同的语言。另一方面,若您在使用 Python 且想知道关于语言特定领域的准确规则,那么一定能在这里找到它们。若愿意看到语言的更正式定义,也许可以贡献您的时间 — 或发明克隆机器。

为语言参考文档添加太多实现细节是危险的 — 实现可能改变,且同一语言的其它实现工作方式也可能不同。另一方面,CPython 是广泛使用的 Python 实现 (尽管替代实现继续获得支持),且其特殊怪癖有时值得一提,尤其是在实现有施加额外局限性的情况下。因此,您会发现整个文本中散布着的简短 "实现注意事项"。

每种 Python 实现都带有许多内置和标准模块。这些文档化在 Python 标准库 。一些内置模块会被提及,当它们与语言定义以显著方式交互时。

1.1. 替代实现

尽管有一种 Python 实现是迄今为止最流行的,但还有一些替代实现对不同受众尤其感兴趣。

已知实现包括:

CPython
This is the original and most-maintained implementation of Python, written in C. New language features generally appear here first.
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. It is also often used to create tests for Java libraries. More information can be found at Jython 网站 .
Python for .NET
This implementation actually uses the CPython implementation, but is a managed .NET application and makes .NET libraries available. It was created by Brian Lloyd. For more information, see the 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. It was created by Jim Hugunin, the original creator of Jython. For more information, see IronPython 网站 .
PyPy
An implementation of Python written completely in 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). Additional information is available on PyPy 工程首页 .

这些中的每个实现以某种方式异于此手册中记录的语言,或引入超出标准 Python 文档编制涵盖的特定信息。请参考特定实现文档编制,以确定需要知道关于正使用的特定实现的什么。

1.2. 表示法

词法分析和句法的描述是使用修改后的 BNF 语法表示法。这使用以下定义样式:

name      ::=  lc_letter (lc_letter | "_")*
lc_letter ::=  "a"..."z"
					

第一行说 name lc_letter followed by a sequence of zero or more lc_letter s and underscores. An lc_letter in turn is any of the single characters 'a' through '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. 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.

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.

内容表

上一话题

Python 语言参考

下一话题

2. 词法分析

本页