399 lines
28 KiB
HTML
399 lines
28 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>reprlib --- 另一种 repr() 实现 — 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="enum --- 对枚举的支持" href="enum.html" />
|
|
<link rel="prev" title="pprint --- 数据美化输出" href="pprint.html" />
|
|
<link rel="canonical" href="https://docs.python.org/3/library/reprlib.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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> --- 另一种 <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> 实现</a><ul>
|
|
<li><a class="reference internal" href="#repr-objects">Repr 对象</a></li>
|
|
<li><a class="reference internal" href="#subclassing-repr-objects">子类化 Repr 对象</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>上一个主题</h4>
|
|
<p class="topless"><a href="pprint.html"
|
|
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> --- 数据美化输出</a></p>
|
|
<h4>下一个主题</h4>
|
|
<p class="topless"><a href="enum.html"
|
|
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">enum</span></code> --- 对枚举的支持</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/reprlib.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/reprlib.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="enum.html" title="enum --- 对枚举的支持"
|
|
accesskey="N">下一页</a> |</li>
|
|
<li class="right" >
|
|
<a href="pprint.html" title="pprint --- 数据美化输出"
|
|
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> »</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> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">数据类型</a> »</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-reprlib">
|
|
<span id="reprlib-alternate-repr-implementation"></span><h1><a class="reference internal" href="#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code></a> --- 另一种 <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 实现<a class="headerlink" href="#module-reprlib" title="永久链接至标题">¶</a></h1>
|
|
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/reprlib.py">Lib/reprlib.py</a></p>
|
|
<hr class="docutils" />
|
|
<p><a class="reference internal" href="#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code></a> 模块提供了一种对象表示的产生方式,它会对结果字符串的大小进行限制。 该方式被用于 Python 调试器,也适用于某些其他场景。</p>
|
|
<p>此模块提供了一个类、一个实例和一个函数:</p>
|
|
<dl class="class">
|
|
<dt id="reprlib.Repr">
|
|
<em class="property">class </em><code class="sig-prename descclassname">reprlib.</code><code class="sig-name descname">Repr</code><a class="headerlink" href="#reprlib.Repr" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>该类提供了格式化服务 适用于实现与内置 <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 相似的方法;其中附加了针对不同对象类型的大小限制,以避免生成超长的表示。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="data">
|
|
<dt id="reprlib.aRepr">
|
|
<code class="sig-prename descclassname">reprlib.</code><code class="sig-name descname">aRepr</code><a class="headerlink" href="#reprlib.aRepr" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>这是 <a class="reference internal" href="#reprlib.Repr" title="reprlib.Repr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Repr</span></code></a> 的一个实例,用于提供如下所述的 <a class="reference internal" href="#reprlib.repr" title="reprlib.repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 函数。 改变此对象的属性将会影响 <a class="reference internal" href="#reprlib.repr" title="reprlib.repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 和 Python 调试器所使用的大小限制。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="function">
|
|
<dt id="reprlib.repr">
|
|
<code class="sig-prename descclassname">reprlib.</code><code class="sig-name descname">repr</code><span class="sig-paren">(</span><em class="sig-param">obj</em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.repr" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>这是 <code class="docutils literal notranslate"><span class="pre">aRepr</span></code> 的 <a class="reference internal" href="#reprlib.Repr.repr" title="reprlib.Repr.repr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">repr()</span></code></a> 方法。 它会返回与同名内置函数所返回字符串相似的字符串,区别在于附带了对多数类型的大小限制。</p>
|
|
</dd></dl>
|
|
|
|
<p>在大小限制工具以外,此模块还提供了一个装饰器,用于检测对 <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> 的递归调用并改用一个占位符来替换。</p>
|
|
<span class="target" id="index-0"></span><dl class="function">
|
|
<dt id="reprlib.recursive_repr">
|
|
<code class="sig-prename descclassname">@</code><code class="sig-prename descclassname">reprlib.</code><code class="sig-name descname">recursive_repr</code><span class="sig-paren">(</span><em class="sig-param">fillvalue="..."</em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.recursive_repr" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>用于为 <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> 方法检测同一线程内部递归调用的装饰器。 如果执行了递归调用,则会返回 <em>fillvalue</em>,否则执行正常的 <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> 调用。 例如:</p>
|
|
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">reprlib</span> <span class="kn">import</span> <span class="n">recursive_repr</span>
|
|
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyList</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="nd">@recursive_repr</span><span class="p">()</span>
|
|
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|
<span class="gp">... </span> <span class="k">return</span> <span class="s1">'<'</span> <span class="o">+</span> <span class="s1">'|'</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="p">))</span> <span class="o">+</span> <span class="s1">'>'</span>
|
|
<span class="gp">...</span>
|
|
<span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">MyList</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
|
|
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
|
|
<span class="go"><'a'|'b'|'c'|...|'x'></span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="versionadded">
|
|
<p><span class="versionmodified added">3.2 新版功能.</span></p>
|
|
</div>
|
|
</dd></dl>
|
|
|
|
<section id="repr-objects">
|
|
<span id="id1"></span><h2>Repr 对象<a class="headerlink" href="#repr-objects" title="永久链接至标题">¶</a></h2>
|
|
<p><a class="reference internal" href="#reprlib.Repr" title="reprlib.Repr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Repr</span></code></a> 实例对象包含一些属性可以用于为不同对象类型的表示提供大小限制,还包含一些方法可以格式化特定的对象类型。</p>
|
|
<dl class="attribute">
|
|
<dt id="reprlib.Repr.maxlevel">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxlevel</code><a class="headerlink" href="#reprlib.Repr.maxlevel" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>创建递归表示形式的深度限制。 默认为 <code class="docutils literal notranslate"><span class="pre">6</span></code>。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="reprlib.Repr.maxdict">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxdict</code><a class="headerlink" href="#reprlib.Repr.maxdict" title="永久链接至目标">¶</a></dt>
|
|
<dt id="reprlib.Repr.maxlist">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxlist</code><a class="headerlink" href="#reprlib.Repr.maxlist" title="永久链接至目标">¶</a></dt>
|
|
<dt id="reprlib.Repr.maxtuple">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxtuple</code><a class="headerlink" href="#reprlib.Repr.maxtuple" title="永久链接至目标">¶</a></dt>
|
|
<dt id="reprlib.Repr.maxset">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxset</code><a class="headerlink" href="#reprlib.Repr.maxset" title="永久链接至目标">¶</a></dt>
|
|
<dt id="reprlib.Repr.maxfrozenset">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxfrozenset</code><a class="headerlink" href="#reprlib.Repr.maxfrozenset" title="永久链接至目标">¶</a></dt>
|
|
<dt id="reprlib.Repr.maxdeque">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxdeque</code><a class="headerlink" href="#reprlib.Repr.maxdeque" title="永久链接至目标">¶</a></dt>
|
|
<dt id="reprlib.Repr.maxarray">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxarray</code><a class="headerlink" href="#reprlib.Repr.maxarray" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>表示命名对象类型的条目数量限制。 对于 <a class="reference internal" href="#reprlib.Repr.maxdict" title="reprlib.Repr.maxdict"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maxdict</span></code></a> 的默认值为 <code class="docutils literal notranslate"><span class="pre">4</span></code>,对于 <a class="reference internal" href="#reprlib.Repr.maxarray" title="reprlib.Repr.maxarray"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maxarray</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">5</span></code>,对于其他则为 <code class="docutils literal notranslate"><span class="pre">6</span></code>。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="reprlib.Repr.maxlong">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxlong</code><a class="headerlink" href="#reprlib.Repr.maxlong" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>表示整数的最大字符数量。 数码会从中间被丢弃。 默认值为 <code class="docutils literal notranslate"><span class="pre">40</span></code>。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="reprlib.Repr.maxstring">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxstring</code><a class="headerlink" href="#reprlib.Repr.maxstring" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>表示字符串的字符数量限制。 请注意字符源会使用字符串的“正常”表示形式:如果表示中需要用到转义序列,在缩短表示时它们可能会被破坏。 默认值为 <code class="docutils literal notranslate"><span class="pre">30</span></code>。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="attribute">
|
|
<dt id="reprlib.Repr.maxother">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">maxother</code><a class="headerlink" href="#reprlib.Repr.maxother" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>此限制用于控制在 <a class="reference internal" href="#reprlib.Repr" title="reprlib.Repr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Repr</span></code></a> 对象上没有特定的格式化方法可用的对象类型的大小。 它会以类似 <a class="reference internal" href="#reprlib.Repr.maxstring" title="reprlib.Repr.maxstring"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maxstring</span></code></a> 的方式被应用。 默认值为 <code class="docutils literal notranslate"><span class="pre">20</span></code>。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="reprlib.Repr.repr">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">repr</code><span class="sig-paren">(</span><em class="sig-param">obj</em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.Repr.repr" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>内置 <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> 的等价形式,它使用实例专属的格式化。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt id="reprlib.Repr.repr1">
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">repr1</code><span class="sig-paren">(</span><em class="sig-param">obj</em>, <em class="sig-param">level</em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.Repr.repr1" title="永久链接至目标">¶</a></dt>
|
|
<dd><p>供 <a class="reference internal" href="#reprlib.Repr.repr" title="reprlib.Repr.repr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">repr()</span></code></a> 使用的递归实现。 此方法使用 <em>obj</em> 的类型来确定要调用哪个格式化方法,并传入 <em>obj</em> 和 <em>level</em>。 类型专属的方法应当调用 <a class="reference internal" href="#reprlib.Repr.repr1" title="reprlib.Repr.repr1"><code class="xref py py-meth docutils literal notranslate"><span class="pre">repr1()</span></code></a> 来执行递归格式化,在递归调用中使用 <code class="docutils literal notranslate"><span class="pre">level</span> <span class="pre">-</span> <span class="pre">1</span></code> 作为 <em>level</em> 的值。</p>
|
|
</dd></dl>
|
|
|
|
<dl class="method">
|
|
<dt>
|
|
<code class="sig-prename descclassname">Repr.</code><code class="sig-name descname">repr_TYPE</code><span class="sig-paren">(</span><em class="sig-param">obj</em>, <em class="sig-param">level</em><span class="sig-paren">)</span></dt>
|
|
<dd><p>特定类型的格式化方法会被实现为基于类型名称来命名的方法。 在方法名称中,<strong>TYPE</strong> 会被替换为 <code class="docutils literal notranslate"><span class="pre">'_'.join(type(obj).__name__.split())</span></code>。 对这些方法的分派会由 <a class="reference internal" href="#reprlib.Repr.repr1" title="reprlib.Repr.repr1"><code class="xref py py-meth docutils literal notranslate"><span class="pre">repr1()</span></code></a> 来处理。 需要对值进行递归格式化的类型专属方法应当调用 <code class="docutils literal notranslate"><span class="pre">self.repr1(subobj,</span> <span class="pre">level</span> <span class="pre">-</span> <span class="pre">1)</span></code>。</p>
|
|
</dd></dl>
|
|
|
|
</section>
|
|
<section id="subclassing-repr-objects">
|
|
<span id="subclassing-reprs"></span><h2>子类化 Repr 对象<a class="headerlink" href="#subclassing-repr-objects" title="永久链接至标题">¶</a></h2>
|
|
<p>通过 <a class="reference internal" href="#reprlib.Repr.repr1" title="reprlib.Repr.repr1"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Repr.repr1()</span></code></a> 使用动态分派允许 <a class="reference internal" href="#reprlib.Repr" title="reprlib.Repr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Repr</span></code></a> 的子类添加对额外内置对象类型的支持,或是修改对已支持类型的处理。 这个例子演示了如何添加对文件对象的特殊支持:</p>
|
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">reprlib</span>
|
|
<span class="kn">import</span> <span class="nn">sys</span>
|
|
|
|
<span class="k">class</span> <span class="nc">MyRepr</span><span class="p">(</span><span class="n">reprlib</span><span class="o">.</span><span class="n">Repr</span><span class="p">):</span>
|
|
|
|
<span class="k">def</span> <span class="nf">repr_TextIOWrapper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">,</span> <span class="n">level</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="n">obj</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="p">{</span><span class="s1">'<stdin>'</span><span class="p">,</span> <span class="s1">'<stdout>'</span><span class="p">,</span> <span class="s1">'<stderr>'</span><span class="p">}:</span>
|
|
<span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">name</span>
|
|
<span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
|
|
|
|
<span class="n">aRepr</span> <span class="o">=</span> <span class="n">MyRepr</span><span class="p">()</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="n">aRepr</span><span class="o">.</span><span class="n">repr</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdin</span><span class="p">))</span> <span class="c1"># prints '<stdin>'</span>
|
|
</pre></div>
|
|
</div>
|
|
</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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> --- 另一种 <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> 实现</a><ul>
|
|
<li><a class="reference internal" href="#repr-objects">Repr 对象</a></li>
|
|
<li><a class="reference internal" href="#subclassing-repr-objects">子类化 Repr 对象</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h4>上一个主题</h4>
|
|
<p class="topless"><a href="pprint.html"
|
|
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> --- 数据美化输出</a></p>
|
|
<h4>下一个主题</h4>
|
|
<p class="topless"><a href="enum.html"
|
|
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">enum</span></code> --- 对枚举的支持</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/reprlib.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="enum.html" title="enum --- 对枚举的支持"
|
|
>下一页</a> |</li>
|
|
<li class="right" >
|
|
<a href="pprint.html" title="pprint --- 数据美化输出"
|
|
>上一页</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> »</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> »
|
|
</li>
|
|
|
|
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
|
<li class="nav-item nav-item-2"><a href="datatypes.html" >数据类型</a> »</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">
|
|
© <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> |