Files
hustoj/web/python-docs/reference/introduction.html
2025-03-09 17:10:56 +08:00

304 lines
18 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>1. 概述 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="2. 词法分析" href="lexical_analysis.html" />
<link rel="prev" title="Python语言参考" href="index.html" />
<link rel="canonical" href="https://docs.python.org/3/reference/introduction.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">1. 概述</a><ul>
<li><a class="reference internal" href="#alternate-implementations">1.1. 其他实现</a></li>
<li><a class="reference internal" href="#notation">1.2. 标注</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="index.html"
title="上一章">Python语言参考</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="lexical_analysis.html"
title="下一章"><span class="section-number">2. </span>词法分析</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/reference/introduction.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/reference/introduction.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="lexical_analysis.html" title="2. 词法分析"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="index.html" title="Python语言参考"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python语言参考</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="introduction">
<span id="id1"></span><h1><span class="section-number">1. </span>概述<a class="headerlink" href="#introduction" title="永久链接至标题"></a></h1>
<p>本参考手册是对 Python 编程语言的描述。并不适宜作为教程使用。</p>
<p>我希望尽可能地保证内容精确无误,但还是选择使用自然词句进行描述,正式的规格定义仅用于句法和词法解析。这样应该能使文档对于普通人来说更易理解,但也可能导致一些歧义。因此,如果你是来自火星并且想凭借这份文档把 Python 重新实现一遍,也许有时需要自行猜测,实际上最终大概会得到一个十分不同的语言。而在另一方面,如果你正在使用 Python 并且想了解有关该语言特定领域的精确规则,你应该能够在这里找到它们。如果你希望查看对该语言更正式的定义,也许你可以花些时间自己写上一份 --- 或者发明一台克隆机器 :-)</p>
<p>在语言参考文档里加入过多的实现细节是很危险的 --- 具体实现可能发生改变对同一语言的其他实现可能使用不同的方式。而在另一方面CPython 是得到广泛使用的 Python 实现 (然而其他一些实现的拥护者也在增加),其中的特殊细节有时也值得一提,特别是当其实现方式导致额外的限制时。因此,你会发现在正文里不时会跳出来一些简短的 &quot;实现注释&quot;</p>
<p>每种 Python 实现都带有一些内置和标准的模块。相关的文档可参见 <a class="reference internal" href="../library/index.html#library-index"><span class="std std-ref">Python 标准库</span></a> 索引。少数内置模块也会在此提及,如果它们同语言描述存在明显的关联。</p>
<section id="alternate-implementations">
<span id="implementations"></span><h2><span class="section-number">1.1. </span>其他实现<a class="headerlink" href="#alternate-implementations" title="永久链接至标题"></a></h2>
<p>虽然官方 Python 实现差不多得到最广泛的欢迎,但也有一些其他实现对特定领域的用户来说更具吸引力。</p>
<p>知名的实现包括:</p>
<dl class="simple">
<dt>CPython</dt><dd><p>这是最早出现并持续维护的 Python 实现,以 C 语言编写。新的语言特性通常在此率先添加。</p>
</dd>
<dt>Jython</dt><dd><p>以 Java 语言编写的 Python 实现。此实现可以作为 Java 应用的一个脚本语言,或者可以用来创建需要 Java 类库支持的应用。想了解更多信息可访问 <a class="reference external" href="http://www.jython.org/">Jython 网站</a></p>
</dd>
<dt>Python for .NET</dt><dd><p>此实现实际上使用了 CPython 实现,但是属于 .NET 托管应用并且可以引入 .NET 类库。它的创造者是 Brian Lloyd。想了解详情可访问 <a class="reference external" href="https://pythonnet.github.io/">Python for .NET 主页</a></p>
</dd>
<dt>IronPython</dt><dd><p>另一个 .NET 的 Python 实现,与 Python.NET 不同点在于它是生成 IL 的完全 Python 实现,并且将 Python 代码直接编译为 .NET 程序集。它的创造者就是当初创造 Jython 的 Jim Hugunin。想了解详情可访问 <a class="reference external" href="http://ironpython.net/">IronPython 网站</a></p>
</dd>
<dt>PyPy</dt><dd><p>完全使用 Python 语言编写的 Python 实现。它支持多个其他实现所没有的高级特性,例如非栈式支持和 JIT 编译器等。此项目的目标之一是通过允许方便地修改解释器 (因为它是用 Python 编写的),鼓励该对语言本身进行试验。想了解详情可访问 <a class="reference external" href="http://pypy.org/">PyPy 项目主页</a></p>
</dd>
</dl>
<p>以上这些实现都可能在某些方面与此参考文档手册的描述有所差异,或是引入了超出标准 Python 文档范围的特定信息。请参考它们各自的专门文档,以确定你正在使用的这个实现有哪些你需要了解的东西。</p>
</section>
<section id="notation">
<span id="id2"></span><h2><span class="section-number">1.2. </span>标注<a class="headerlink" href="#notation" title="永久链接至标题"></a></h2>
<p id="index-0">句法和词法解析的描述采用经过改进的 BNF 语法标注。这包含以下定义样式:</p>
<pre>
<strong id="grammar-token-name">name </strong> ::= <a class="reference internal" href="#grammar-token-lc-letter"><code class="xref docutils literal notranslate"><span class="pre">lc_letter</span></code></a> (<a class="reference internal" href="#grammar-token-lc-letter"><code class="xref docutils literal notranslate"><span class="pre">lc_letter</span></code></a> | &quot;_&quot;)*
<strong id="grammar-token-lc-letter">lc_letter</strong> ::= &quot;a&quot;...&quot;z&quot;
</pre>
<p>第一行表示 <code class="docutils literal notranslate"><span class="pre">name</span></code> 是一个 <code class="docutils literal notranslate"><span class="pre">lc_letter</span></code> 之后跟零个或多个 <code class="docutils literal notranslate"><span class="pre">lc_letter</span></code> 和下划线。而一个 <code class="docutils literal notranslate"><span class="pre">lc_letter</span></code> 则是任意单个 <code class="docutils literal notranslate"><span class="pre">'a'</span></code><code class="docutils literal notranslate"><span class="pre">'z'</span></code> 字符。(实际上在本文档中始终采用此规则来定义词法和语法规则的名称。)</p>
<p>每条规则的开头是一个名称 (即该规则所定义的名称) 加上 <code class="docutils literal notranslate"><span class="pre">::=</span></code>。 竖线 (<code class="docutils literal notranslate"><span class="pre">|</span></code>) 被用来分隔可选项,它是此标注中绑定程度最低的操作符。 星号 (<code class="docutils literal notranslate"><span class="pre">*</span></code>) 表示前一项的零次或多次重复,类似地,加号 (<code class="docutils literal notranslate"><span class="pre">+</span></code>) 表示一次或多次重复,而由方括号括起的内容 (<code class="docutils literal notranslate"><span class="pre">[</span> <span class="pre">]</span></code>) 表示出现零次或一次 (或者说,这部分内容是可选的)。 <code class="docutils literal notranslate"><span class="pre">*</span></code><code class="docutils literal notranslate"><span class="pre">+</span></code> 操作符的绑定是最紧密的,圆括号用于分组。 字符串字面值包含在引号内。 空格的作用仅限于分隔形符。 每条规则通常为一行,有许多个可选项的规则可能会以竖线为界分为多行。</p>
<p id="index-1">在词法定义中 (如上述示例),还额外使用了两个约定: 由三个点号分隔的两个字符字面值表示在指定 (闭) 区间范围内的任意单个 ASCII 字符。由尖括号 (<code class="docutils literal notranslate"><span class="pre">&lt;...&gt;</span></code>) 括起来的内容是对于所定义符号的非正式描述;即可以在必要时用来说明 '控制字符' 的意图。</p>
<p>虽然所用的标注方式几乎相同,但是词法定义和句法定义是存在很大区别的: 词法定义作用于输入源中单独的字符,而句法定义则作用于由词法分析所生成的形符流。在下一章节 (&quot;词法分析&quot;) 中使用的 BNF 全部都是词法定义;在之后的章节中使用的则是句法定义。</p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">1. 概述</a><ul>
<li><a class="reference internal" href="#alternate-implementations">1.1. 其他实现</a></li>
<li><a class="reference internal" href="#notation">1.2. 标注</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="index.html"
title="上一章">Python语言参考</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="lexical_analysis.html"
title="下一章"><span class="section-number">2. </span>词法分析</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/reference/introduction.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="lexical_analysis.html" title="2. 词法分析"
>下一页</a> |</li>
<li class="right" >
<a href="index.html" title="Python语言参考"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python语言参考</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>