此参考手册描述 Python 编程语言。不打算作为教程。
虽然试着尽可能精确,但除句法和词法分析外,我选择使用英语而不是正式规范。这应该能使普通读者更理解文档,但会为歧义留下空间。因此,若您来自火星并试着仅从此文档重实现 Python,可能不得不猜测事情,且实际上可能最终实现完全不同的语言。另一方面,若您在使用 Python 且想知道关于语言特定领域的精确规则,那么一定能在这里找到它们。若愿意看到语言的更正式定义,也许可以贡献您的时间 — 或发明克隆机器。
为语言参考文档添加太多实现细节是危险的 — 实现可能改变,且同一语言的其它实现工作方式也可能不同。另一方面,CPython 是广泛使用的 Python 实现 (尽管替代实现继续获得支持),且它的特殊特性有时值得一提,尤其是在实现强加额外局限性的情况下。因此,您会发现整个文本中散布着的简短 "实现注意事项"。
每种 Python 实现都带有许多内置和标准模块。这些文档化在 Python 标准库 。一些内置模块会被提及,当它们与语言定义以显著方式交互时。
尽管有一种 Python 实现是迄今为止最流行的,但还有一些替代实现对不同受众尤其感兴趣。
已知实现包括:
这些中的每个实现以某种方式异于此手册中记录的语言,或引入超出标准 Python 文档编制涵盖的特定信息。请参考特定实现文档编制,以确定需要知道关于正使用的特定实现的什么。
词法分析和句法的描述是使用修改后的 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.