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

287 lines
19 KiB
HTML

<!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>pydoc --- 文档生成器和在线帮助系统 &#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="doctest --- 测试交互式的 Python 示例" href="doctest.html" />
<link rel="prev" title="typing —— 对类型提示的支持" href="typing.html" />
<link rel="canonical" href="https://docs.python.org/3/library/pydoc.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>
<h4>上一个主题</h4>
<p class="topless"><a href="typing.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code> —— 对类型提示的支持</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="doctest.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> --- 测试交互式的 Python 示例</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/library/pydoc.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/library/pydoc.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="doctest.html" title="doctest --- 测试交互式的 Python 示例"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="typing.html" title="typing —— 对类型提示的支持"
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" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="development.html" accesskey="U">开发工具</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="module-pydoc">
<span id="pydoc-documentation-generator-and-online-help-system"></span><h1><a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> --- 文档生成器和在线帮助系统<a class="headerlink" href="#module-pydoc" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/pydoc.py">Lib/pydoc.py</a></p>
<hr class="docutils" id="index-0" />
<p><a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> 模块自动利用 Python 模块生成文档。生成的文档可在控制台中显示为文本页面,还可在 Web 浏览器中查阅,或保存为 HTML 文件。</p>
<p>对于模块、类、函数和方法,显示的文档内容取自文档字符串(即 <code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code> 属性),并会递归地从其带文档的成员中获取。 如果没有文档字符串,<a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> 会尝试从类、函数或方法定义上方,或是模块顶部的注释行段落获取 (参见 <a class="reference internal" href="inspect.html#inspect.getcomments" title="inspect.getcomments"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.getcomments()</span></code></a>).</p>
<p>内置函数 <a class="reference internal" href="functions.html#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a> 会发起调用交互式解释器的在线帮助系统,该系统使用 <a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> 在终端上生成文本形式的文档内容。 同样的文本文档也可以在 Python 解释器以外通过在操作系统的命令提示符下以脚本方式运行 <strong class="program">pydoc</strong> 来查看。 例如,运行</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">pydoc</span> <span class="n">sys</span>
</pre></div>
</div>
<p>在终端提示符下将通过 <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> 模块显示文档内容,其样式类似于 Unix <strong class="program">man</strong> 命令所显示的指南页面。 <strong class="program">pydoc</strong> 的参数可以为函数、模块、包,或带点号的对模块中的类、方法或函数以及包中的模块的引用。 如果传给 <strong class="program">pydoc</strong> 的参数像是一个路径(即包含所在操作系统的路径分隔符,例如 Unix 的正斜杠),并且其指向一个现有的 Python 源文件,则会为该文件生成文档内容。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>为了找到对象及其文档内容,<a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> 会导入文档所在的模块。 因此,任何模块层级的代码都将被执行。 请使用 <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">'__main__':</span></code> 语句来确保一个文件的特定代码仅在作为脚本被发起调用时执行而不是在被导入时执行。</p>
</div>
<p>当打印输出到控制台时,<strong class="program">pydoc</strong> 会尝试对输出进行分页以方便阅读。 如果设置了 <span class="target" id="index-5"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PAGER</span></code> 环境变量,<strong class="program">pydoc</strong> 将使用该变量值作为分页程序。</p>
<p>在参数前指定 <code class="docutils literal notranslate"><span class="pre">-w</span></code> 旗标将把 HTML 文档写入到当前目录下的一个文件中,而不是在控制台中显示文本。</p>
<p>在参数前指定 <code class="docutils literal notranslate"><span class="pre">-k</span></code> 旗标将在全部可用模块的提要行中搜索参数所给定的关键字,具体方式同样类似于 Unix <strong class="program">man</strong> 命令。 模块的提要行就是其文档字符串的第一行。</p>
<p>你还可以使用 <strong class="program">pydoc</strong> 在本机上启动一个 HTTP 服务,这将向来访的 Web 浏览器提供文档服务。 <strong class="program">pydoc -p 1234</strong> 将在 1234 端口上启动 HTTP 服务,允许你在你喜欢的 Web 服务器中通过 <code class="docutils literal notranslate"><span class="pre">http://localhost:1234/</span></code> 浏览文档内容。 指定 <code class="docutils literal notranslate"><span class="pre">0</span></code> 作为端口号将会任意选择一个未使用的端口。</p>
<p><strong class="program">pydoc -n &lt;hostname&gt;</strong> 将启动在给定主机名上执行监听的服务。 默认主机名为 'localhost' 但如果你希望能从其他机器搜索该服务器,你可能会想要改变服务器所响应的主机名。 在开发阶段此特性会特别有用,因为这样你将能在一个容器中运行 pydoc。</p>
<p><strong class="program">pydoc -b</strong> 将启动服务并额外打开一个 Web 浏览器访问模块索引页。 所发布的每个页面顶端都带有导航栏,你可以点击 <em>Get</em> 获取特定条目的帮助,点击 <em>Search</em> 在所有模块的提要行中搜索特定关键词,或是点击 <em>Module index</em>, <em>Topics</em><em>Keywords</em> 前往相应的页面。</p>
<p><strong class="program">pydoc</strong> 生成文档内容时,它会使用当前环境和路径来定位模块。 因此,发起调用 <strong class="program">pydoc spam</strong> 得到的文档版本会与你启动 Python 解释器并输入 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">spam</span></code> 时得到的模块版本完全相同。</p>
<p>核心模块的模块文档位置对应于 <code class="docutils literal notranslate"><span class="pre">https://docs.python.org/X.Y/library/</span></code> 其中 <code class="docutils literal notranslate"><span class="pre">X</span></code><code class="docutils literal notranslate"><span class="pre">Y</span></code> 是 Python 解释器的主要版本号和小版本号。 这可通过设置 <span class="target" id="index-6"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDOCS</span></code> 环境变量来重载为指向不同的 URL 或包含 Library Reference Manual 页面的本地目录。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加 <code class="docutils literal notranslate"><span class="pre">-b</span></code> 选项。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.3 版更改: </span>命令行选项 <code class="docutils literal notranslate"><span class="pre">-g</span></code> 已经移除。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span><a class="reference internal" href="#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> 现在会使用 <a class="reference internal" href="inspect.html#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.signature()</span></code></a> 而非 <a class="reference internal" href="inspect.html#inspect.getfullargspec" title="inspect.getfullargspec"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.getfullargspec()</span></code></a> 来从可调用对象中提取签名信息。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>添加 <code class="docutils literal notranslate"><span class="pre">-n</span></code> 选项。</p>
</div>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>上一个主题</h4>
<p class="topless"><a href="typing.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code> —— 对类型提示的支持</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="doctest.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code> --- 测试交互式的 Python 示例</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/library/pydoc.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="doctest.html" title="doctest --- 测试交互式的 Python 示例"
>下一页</a> |</li>
<li class="right" >
<a href="typing.html" title="typing —— 对类型提示的支持"
>上一页</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="nav-item nav-item-2"><a href="development.html" >开发工具</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>