gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

579 lines
43KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="reprlib — Alternate repr() implementation" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/reprlib.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/reprlib.py The reprlib module provides a means for producing object representations with limits on the size of the resulting strings. This is used in the Python debugger and may be..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/reprlib.py The reprlib module provides a means for producing object representations with limits on the size of the resulting strings. This is used in the Python debugger and may be..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>reprlib — Alternate repr() implementation &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="enum — Support for enumerations" href="enum.html" />
  33. <link rel="prev" title="pprint — Data pretty printer" href="pprint.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/reprlib.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" 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"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> — Alternate <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> implementation</a><ul>
  86. <li><a class="reference internal" href="#repr-objects">Repr Objects</a></li>
  87. <li><a class="reference internal" href="#subclassing-repr-objects">Subclassing Repr Objects</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="pprint.html"
  95. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="enum.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">enum</span></code> — Support for enumerations</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/reprlib.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="enum.html" title="enum — Support for enumerations"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="pprint.html" title="pprint — Data pretty printer"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">Data Types</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> — Alternate <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> implementation</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-reprlib">
  170. <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> — Alternate <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> implementation<a class="headerlink" href="#module-reprlib" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/reprlib.py">Lib/reprlib.py</a></p>
  172. <hr class="docutils" />
  173. <p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> module provides a means for producing object representations
  174. with limits on the size of the resulting strings. This is used in the Python
  175. debugger and may be useful in other contexts as well.</p>
  176. <p>This module provides a class, an instance, and a function:</p>
  177. <dl class="py class">
  178. <dt class="sig sig-object py" id="reprlib.Repr">
  179. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">reprlib.</span></span><span class="sig-name descname"><span class="pre">Repr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxlevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">6</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxtuple</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">6</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxlist</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">6</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxarray</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">5</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxdict</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">4</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">6</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxfrozenset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">6</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxdeque</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">6</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxstring</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">30</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxlong</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">40</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxother</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">30</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fillvalue</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'...'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.Repr" title="Link to this definition">¶</a></dt>
  180. <dd><p>Class which provides formatting services useful in implementing functions
  181. similar to the built-in <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>; size limits for different object types
  182. are added to avoid the generation of representations which are excessively long.</p>
  183. <p>The keyword arguments of the constructor can be used as a shortcut to set the
  184. attributes of the <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> instance. Which means that the following
  185. initialization:</p>
  186. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">aRepr</span> <span class="o">=</span> <span class="n">reprlib</span><span class="o">.</span><span class="n">Repr</span><span class="p">(</span><span class="n">maxlevel</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
  187. </pre></div>
  188. </div>
  189. <p>Is equivalent to:</p>
  190. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">aRepr</span> <span class="o">=</span> <span class="n">reprlib</span><span class="o">.</span><span class="n">Repr</span><span class="p">()</span>
  191. <span class="n">aRepr</span><span class="o">.</span><span class="n">maxlevel</span> <span class="o">=</span> <span class="mi">3</span>
  192. </pre></div>
  193. </div>
  194. <p>See section <a class="reference internal" href="#id1">Repr Objects</a> for more information about <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>
  195. attributes.</p>
  196. <div class="versionchanged">
  197. <p><span class="versionmodified changed">Changed in version 3.12: </span>Allow attributes to be set via keyword arguments.</p>
  198. </div>
  199. </dd></dl>
  200. <dl class="py data">
  201. <dt class="sig sig-object py" id="reprlib.aRepr">
  202. <span class="sig-prename descclassname"><span class="pre">reprlib.</span></span><span class="sig-name descname"><span class="pre">aRepr</span></span><a class="headerlink" href="#reprlib.aRepr" title="Link to this definition">¶</a></dt>
  203. <dd><p>This is an instance of <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> which is used to provide the
  204. <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> function described below. Changing the attributes of this
  205. object will affect the size limits used by <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> and the Python
  206. debugger.</p>
  207. </dd></dl>
  208. <dl class="py function">
  209. <dt class="sig sig-object py" id="reprlib.repr">
  210. <span class="sig-prename descclassname"><span class="pre">reprlib.</span></span><span class="sig-name descname"><span class="pre">repr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.repr" title="Link to this definition">¶</a></dt>
  211. <dd><p>This is the <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> method of <code class="docutils literal notranslate"><span class="pre">aRepr</span></code>. It returns a string
  212. similar to that returned by the built-in function of the same name, but with
  213. limits on most sizes.</p>
  214. </dd></dl>
  215. <p>In addition to size-limiting tools, the module also provides a decorator for
  216. detecting recursive calls to <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> and substituting a
  217. placeholder string instead.</p>
  218. <dl class="py function" id="index-0">
  219. <dt class="sig sig-object py" id="reprlib.recursive_repr">
  220. <span class="sig-prename descclassname"><span class="pre">&#64;</span></span><span class="sig-prename descclassname"><span class="pre">reprlib.</span></span><span class="sig-name descname"><span class="pre">recursive_repr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fillvalue</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'...'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.recursive_repr" title="Link to this definition">¶</a></dt>
  221. <dd><p>Decorator for <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> methods to detect recursive calls within the
  222. same thread. If a recursive call is made, the <em>fillvalue</em> is returned,
  223. otherwise, the usual <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> call is made. For example:</p>
  224. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">reprlib</span> <span class="kn">import</span> <span class="n">recursive_repr</span>
  225. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MyList</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>
  226. <span class="gp">... </span> <span class="nd">@recursive_repr</span><span class="p">()</span>
  227. <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>
  228. <span class="gp">... </span> <span class="k">return</span> <span class="s1">&#39;&lt;&#39;</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</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">&#39;&gt;&#39;</span>
  229. <span class="gp">...</span>
  230. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">MyList</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>
  231. <span class="gp">&gt;&gt;&gt; </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>
  232. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
  233. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
  234. <span class="go">&lt;&#39;a&#39;|&#39;b&#39;|&#39;c&#39;|...|&#39;x&#39;&gt;</span>
  235. </pre></div>
  236. </div>
  237. <div class="versionadded">
  238. <p><span class="versionmodified added">New in version 3.2.</span></p>
  239. </div>
  240. </dd></dl>
  241. <section id="repr-objects">
  242. <span id="id1"></span><h2>Repr Objects<a class="headerlink" href="#repr-objects" title="Link to this heading">¶</a></h2>
  243. <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> instances provide several attributes which can be used to provide
  244. size limits for the representations of different object types, and methods
  245. which format specific object types.</p>
  246. <dl class="py attribute">
  247. <dt class="sig sig-object py" id="reprlib.Repr.fillvalue">
  248. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">fillvalue</span></span><a class="headerlink" href="#reprlib.Repr.fillvalue" title="Link to this definition">¶</a></dt>
  249. <dd><p>This string is displayed for recursive references. It defaults to
  250. <code class="docutils literal notranslate"><span class="pre">...</span></code>.</p>
  251. <div class="versionadded">
  252. <p><span class="versionmodified added">New in version 3.11.</span></p>
  253. </div>
  254. </dd></dl>
  255. <dl class="py attribute">
  256. <dt class="sig sig-object py" id="reprlib.Repr.maxlevel">
  257. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxlevel</span></span><a class="headerlink" href="#reprlib.Repr.maxlevel" title="Link to this definition">¶</a></dt>
  258. <dd><p>Depth limit on the creation of recursive representations. The default is <code class="docutils literal notranslate"><span class="pre">6</span></code>.</p>
  259. </dd></dl>
  260. <dl class="py attribute">
  261. <dt class="sig sig-object py" id="reprlib.Repr.maxdict">
  262. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxdict</span></span><a class="headerlink" href="#reprlib.Repr.maxdict" title="Link to this definition">¶</a></dt>
  263. <dt class="sig sig-object py" id="reprlib.Repr.maxlist">
  264. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxlist</span></span><a class="headerlink" href="#reprlib.Repr.maxlist" title="Link to this definition">¶</a></dt>
  265. <dt class="sig sig-object py" id="reprlib.Repr.maxtuple">
  266. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxtuple</span></span><a class="headerlink" href="#reprlib.Repr.maxtuple" title="Link to this definition">¶</a></dt>
  267. <dt class="sig sig-object py" id="reprlib.Repr.maxset">
  268. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxset</span></span><a class="headerlink" href="#reprlib.Repr.maxset" title="Link to this definition">¶</a></dt>
  269. <dt class="sig sig-object py" id="reprlib.Repr.maxfrozenset">
  270. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxfrozenset</span></span><a class="headerlink" href="#reprlib.Repr.maxfrozenset" title="Link to this definition">¶</a></dt>
  271. <dt class="sig sig-object py" id="reprlib.Repr.maxdeque">
  272. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxdeque</span></span><a class="headerlink" href="#reprlib.Repr.maxdeque" title="Link to this definition">¶</a></dt>
  273. <dt class="sig sig-object py" id="reprlib.Repr.maxarray">
  274. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxarray</span></span><a class="headerlink" href="#reprlib.Repr.maxarray" title="Link to this definition">¶</a></dt>
  275. <dd><p>Limits on the number of entries represented for the named object type. The
  276. default is <code class="docutils literal notranslate"><span class="pre">4</span></code> for <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">5</span></code> for <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>, and <code class="docutils literal notranslate"><span class="pre">6</span></code> for
  277. the others.</p>
  278. </dd></dl>
  279. <dl class="py attribute">
  280. <dt class="sig sig-object py" id="reprlib.Repr.maxlong">
  281. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxlong</span></span><a class="headerlink" href="#reprlib.Repr.maxlong" title="Link to this definition">¶</a></dt>
  282. <dd><p>Maximum number of characters in the representation for an integer. Digits
  283. are dropped from the middle. The default is <code class="docutils literal notranslate"><span class="pre">40</span></code>.</p>
  284. </dd></dl>
  285. <dl class="py attribute">
  286. <dt class="sig sig-object py" id="reprlib.Repr.maxstring">
  287. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxstring</span></span><a class="headerlink" href="#reprlib.Repr.maxstring" title="Link to this definition">¶</a></dt>
  288. <dd><p>Limit on the number of characters in the representation of the string. Note
  289. that the “normal” representation of the string is used as the character source:
  290. if escape sequences are needed in the representation, these may be mangled when
  291. the representation is shortened. The default is <code class="docutils literal notranslate"><span class="pre">30</span></code>.</p>
  292. </dd></dl>
  293. <dl class="py attribute">
  294. <dt class="sig sig-object py" id="reprlib.Repr.maxother">
  295. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">maxother</span></span><a class="headerlink" href="#reprlib.Repr.maxother" title="Link to this definition">¶</a></dt>
  296. <dd><p>This limit is used to control the size of object types for which no specific
  297. formatting method is available on the <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> object. It is applied in a
  298. similar manner as <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>. The default is <code class="docutils literal notranslate"><span class="pre">20</span></code>.</p>
  299. </dd></dl>
  300. <dl class="py attribute">
  301. <dt class="sig sig-object py" id="reprlib.Repr.indent">
  302. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">indent</span></span><a class="headerlink" href="#reprlib.Repr.indent" title="Link to this definition">¶</a></dt>
  303. <dd><p>If this attribute is set to <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), the output is formatted
  304. with no line breaks or indentation, like the standard <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>.
  305. For example:</p>
  306. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">example</span> <span class="o">=</span> <span class="p">[</span>
  307. <span class="gp">... </span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;spam&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="s1">&#39;spam eggs&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span> <span class="p">{</span><span class="mi">3</span><span class="p">:</span> <span class="mf">4.5</span><span class="p">,</span> <span class="mi">6</span><span class="p">:</span> <span class="p">[]}},</span> <span class="s1">&#39;ham&#39;</span><span class="p">]</span>
  308. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">reprlib</span>
  309. <span class="gp">&gt;&gt;&gt; </span><span class="n">aRepr</span> <span class="o">=</span> <span class="n">reprlib</span><span class="o">.</span><span class="n">Repr</span><span class="p">()</span>
  310. <span class="gp">&gt;&gt;&gt; </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">example</span><span class="p">))</span>
  311. <span class="go">[1, &#39;spam&#39;, {&#39;a&#39;: 2, &#39;b&#39;: &#39;spam eggs&#39;, &#39;c&#39;: {3: 4.5, 6: []}}, &#39;ham&#39;]</span>
  312. </pre></div>
  313. </div>
  314. <p>If <a class="reference internal" href="#reprlib.Repr.indent" title="reprlib.Repr.indent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">indent</span></code></a> is set to a string, each recursion level
  315. is placed on its own line, indented by that string:</p>
  316. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">aRepr</span><span class="o">.</span><span class="n">indent</span> <span class="o">=</span> <span class="s1">&#39;--&gt;&#39;</span>
  317. <span class="gp">&gt;&gt;&gt; </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">example</span><span class="p">))</span>
  318. <span class="go">[</span>
  319. <span class="go">--&gt;1,</span>
  320. <span class="go">--&gt;&#39;spam&#39;,</span>
  321. <span class="go">--&gt;{</span>
  322. <span class="go">--&gt;--&gt;&#39;a&#39;: 2,</span>
  323. <span class="go">--&gt;--&gt;&#39;b&#39;: &#39;spam eggs&#39;,</span>
  324. <span class="go">--&gt;--&gt;&#39;c&#39;: {</span>
  325. <span class="go">--&gt;--&gt;--&gt;3: 4.5,</span>
  326. <span class="go">--&gt;--&gt;--&gt;6: [],</span>
  327. <span class="go">--&gt;--&gt;},</span>
  328. <span class="go">--&gt;},</span>
  329. <span class="go">--&gt;&#39;ham&#39;,</span>
  330. <span class="go">]</span>
  331. </pre></div>
  332. </div>
  333. <p>Setting <a class="reference internal" href="#reprlib.Repr.indent" title="reprlib.Repr.indent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">indent</span></code></a> to a positive integer value behaves as if it
  334. was set to a string with that number of spaces:</p>
  335. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">aRepr</span><span class="o">.</span><span class="n">indent</span> <span class="o">=</span> <span class="mi">4</span>
  336. <span class="gp">&gt;&gt;&gt; </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">example</span><span class="p">))</span>
  337. <span class="go">[</span>
  338. <span class="go"> 1,</span>
  339. <span class="go"> &#39;spam&#39;,</span>
  340. <span class="go"> {</span>
  341. <span class="go"> &#39;a&#39;: 2,</span>
  342. <span class="go"> &#39;b&#39;: &#39;spam eggs&#39;,</span>
  343. <span class="go"> &#39;c&#39;: {</span>
  344. <span class="go"> 3: 4.5,</span>
  345. <span class="go"> 6: [],</span>
  346. <span class="go"> },</span>
  347. <span class="go"> },</span>
  348. <span class="go"> &#39;ham&#39;,</span>
  349. <span class="go">]</span>
  350. </pre></div>
  351. </div>
  352. <div class="versionadded">
  353. <p><span class="versionmodified added">New in version 3.12.</span></p>
  354. </div>
  355. </dd></dl>
  356. <dl class="py method">
  357. <dt class="sig sig-object py" id="reprlib.Repr.repr">
  358. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">repr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.Repr.repr" title="Link to this definition">¶</a></dt>
  359. <dd><p>The equivalent to the built-in <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> that uses the formatting imposed by
  360. the instance.</p>
  361. </dd></dl>
  362. <dl class="py method">
  363. <dt class="sig sig-object py" id="reprlib.Repr.repr1">
  364. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">repr1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#reprlib.Repr.repr1" title="Link to this definition">¶</a></dt>
  365. <dd><p>Recursive implementation used by <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>. This uses the type of <em>obj</em> to
  366. determine which formatting method to call, passing it <em>obj</em> and <em>level</em>. The
  367. type-specific methods should call <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> to perform recursive formatting,
  368. with <code class="docutils literal notranslate"><span class="pre">level</span> <span class="pre">-</span> <span class="pre">1</span></code> for the value of <em>level</em> in the recursive call.</p>
  369. </dd></dl>
  370. <dl class="py method">
  371. <dt class="sig sig-object py">
  372. <span class="sig-prename descclassname"><span class="pre">Repr.</span></span><span class="sig-name descname"><span class="pre">repr_TYPE</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">level</span></span></em><span class="sig-paren">)</span></dt>
  373. <dd><p>Formatting methods for specific types are implemented as methods with a name
  374. based on the type name. In the method name, <strong>TYPE</strong> is replaced by
  375. <code class="docutils literal notranslate"><span class="pre">'_'.join(type(obj).__name__.split())</span></code>. Dispatch to these methods is
  376. handled by <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>. Type-specific methods which need to recursively
  377. format a value should call <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>
  378. </dd></dl>
  379. </section>
  380. <section id="subclassing-repr-objects">
  381. <span id="subclassing-reprs"></span><h2>Subclassing Repr Objects<a class="headerlink" href="#subclassing-repr-objects" title="Link to this heading">¶</a></h2>
  382. <p>The use of dynamic dispatching by <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> allows subclasses of
  383. <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> to add support for additional built-in object types or to modify
  384. the handling of types already supported. This example shows how special support
  385. for file objects could be added:</p>
  386. <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">reprlib</span>
  387. <span class="kn">import</span> <span class="nn">sys</span>
  388. <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>
  389. <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>
  390. <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">&#39;&lt;stdin&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;stdout&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;stderr&gt;&#39;</span><span class="p">}:</span>
  391. <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">name</span>
  392. <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
  393. <span class="n">aRepr</span> <span class="o">=</span> <span class="n">MyRepr</span><span class="p">()</span>
  394. <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 &#39;&lt;stdin&gt;&#39;</span>
  395. </pre></div>
  396. </div>
  397. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&lt;stdin&gt;
  398. </pre></div>
  399. </div>
  400. </section>
  401. </section>
  402. <div class="clearer"></div>
  403. </div>
  404. </div>
  405. </div>
  406. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  407. <div class="sphinxsidebarwrapper">
  408. <div>
  409. <h3><a href="../contents.html">Table of Contents</a></h3>
  410. <ul>
  411. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> — Alternate <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> implementation</a><ul>
  412. <li><a class="reference internal" href="#repr-objects">Repr Objects</a></li>
  413. <li><a class="reference internal" href="#subclassing-repr-objects">Subclassing Repr Objects</a></li>
  414. </ul>
  415. </li>
  416. </ul>
  417. </div>
  418. <div>
  419. <h4>Previous topic</h4>
  420. <p class="topless"><a href="pprint.html"
  421. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a></p>
  422. </div>
  423. <div>
  424. <h4>Next topic</h4>
  425. <p class="topless"><a href="enum.html"
  426. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">enum</span></code> — Support for enumerations</a></p>
  427. </div>
  428. <div role="note" aria-label="source link">
  429. <h3>This Page</h3>
  430. <ul class="this-page-menu">
  431. <li><a href="../bugs.html">Report a Bug</a></li>
  432. <li>
  433. <a href="https://github.com/python/cpython/blob/main/Doc/library/reprlib.rst"
  434. rel="nofollow">Show Source
  435. </a>
  436. </li>
  437. </ul>
  438. </div>
  439. </div>
  440. <div id="sidebarbutton" title="Collapse sidebar">
  441. <span>«</span>
  442. </div>
  443. </div>
  444. <div class="clearer"></div>
  445. </div>
  446. <div class="related" role="navigation" aria-label="related navigation">
  447. <h3>Navigation</h3>
  448. <ul>
  449. <li class="right" style="margin-right: 10px">
  450. <a href="../genindex.html" title="General Index"
  451. >index</a></li>
  452. <li class="right" >
  453. <a href="../py-modindex.html" title="Python Module Index"
  454. >modules</a> |</li>
  455. <li class="right" >
  456. <a href="enum.html" title="enum — Support for enumerations"
  457. >next</a> |</li>
  458. <li class="right" >
  459. <a href="pprint.html" title="pprint — Data pretty printer"
  460. >previous</a> |</li>
  461. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  462. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  463. <li class="switchers">
  464. <div class="language_switcher_placeholder"></div>
  465. <div class="version_switcher_placeholder"></div>
  466. </li>
  467. <li>
  468. </li>
  469. <li id="cpython-language-and-version">
  470. <a href="../index.html">3.12.3 Documentation</a> &#187;
  471. </li>
  472. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  473. <li class="nav-item nav-item-2"><a href="datatypes.html" >Data Types</a> &#187;</li>
  474. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> — Alternate <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> implementation</a></li>
  475. <li class="right">
  476. <div class="inline-search" role="search">
  477. <form class="inline-search" action="../search.html" method="get">
  478. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  479. <input type="submit" value="Go" />
  480. </form>
  481. </div>
  482. |
  483. </li>
  484. <li class="right">
  485. <label class="theme-selector-label">
  486. Theme
  487. <select class="theme-selector" oninput="activateTheme(this.value)">
  488. <option value="auto" selected>Auto</option>
  489. <option value="light">Light</option>
  490. <option value="dark">Dark</option>
  491. </select>
  492. </label> |</li>
  493. </ul>
  494. </div>
  495. <div class="footer">
  496. &copy;
  497. <a href="../copyright.html">
  498. Copyright
  499. </a>
  500. 2001-2024, Python Software Foundation.
  501. <br />
  502. This page is licensed under the Python Software Foundation License Version 2.
  503. <br />
  504. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  505. <br />
  506. See <a href="/license.html">History and License</a> for more information.<br />
  507. <br />
  508. The Python Software Foundation is a non-profit corporation.
  509. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  510. <br />
  511. <br />
  512. Last updated on Apr 09, 2024 (13:47 UTC).
  513. <a href="/bugs.html">Found a bug</a>?
  514. <br />
  515. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  516. </div>
  517. </body>
  518. </html>
上海开阖软件有限公司 沪ICP备12045867号-1