内容表

  • 1. 介绍
    • 1.1. 替代实现
    • 1.2. 表示法

上一话题

Python 语言参考

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 语言参考
  8. 1. 介绍

1. 介绍 ¶

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

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

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

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

1.1. 替代实现 ¶

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

已知实现包括:

CPython

这是原始且维护最多的 Python 实现,以 C 编写。新的语言特征一般首先出现在这里。

Jython

以 Java 实现的 Python。此实现可用作 Java 应用程序的脚本语言,或可用于创建使用 Java 类库的应用程序。它还常被用于为 Java 库创建测试。可以找到更多信息在 Jython 网站 .

Python for .NET

此实现实际使用 CPython 实现,但它可管理 .NET 应用程序并使 .NET 库可用。由布赖恩劳埃德创建。更多信息,见 Python for .NET 首页 .

IronPython

替代 Python for .NET。不像 Python.NET,这是完整 Python 实现,生成 IL (中间语言) 并将 Python 代码直接编译成 .NET 汇编。更多信息,见 IronPython 网站 .

PyPy

完全以 Python 编写的 Python 实现。它支持在其它实现中找不到的几个高级特征 (像:无堆栈支持和即时编译器)。工程目标之一是通过使修改解释器 (由于它是以 Python 编写的) 变得更容易来鼓励对语言本身的实验。额外可用信息在 PyPy 工程首页 .

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

1.2. Notation ¶

The descriptions of lexical analysis and syntax use a modified Backus–Naur form (BNF) grammar notation. This uses the following style of definition:

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.

内容表

  • 1. 介绍
    • 1.1. 替代实现
    • 1.2. 表示法

上一话题

Python 语言参考

下一话题

2. 词法分析

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 语言参考
  8. 1. 介绍

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1