parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="reprlib — Alternate repr() implementation" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/reprlib.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_reprlib_53854a86.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>reprlib — Alternate repr() implementation — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="enum — Support for enumerations" href="enum.html" />43 <link rel="prev" title="pprint — Data pretty printer" href="pprint.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/reprlib.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#repr-objects">Repr Objects</a></li>109<li><a class="reference internal" href="#subclassing-repr-objects">Subclassing Repr Objects</a></li>110</ul>111</li>112</ul>113 114 </div>115 <div>116 <h4>Previous topic</h4>117 <p class="topless"><a href="pprint.html"118 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a></p>119 </div>120 <div>121 <h4>Next topic</h4>122 <p class="topless"><a href="enum.html"123 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">enum</span></code> — Support for enumerations</a></p>124 </div>125 <script>126 document.addEventListener('DOMContentLoaded', () => {127 const title = document.querySelector('meta[property="og:title"]').content;128 const elements = document.querySelectorAll('.improvepage');129 const pageurl = window.location.href.split('?')[0];130 elements.forEach(element => {131 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));132 url.searchParams.set('pagetitle', title);133 url.searchParams.set('pageurl', pageurl);134 url.searchParams.set('pagesource', "library/reprlib.rst");135 element.href = url.toString();136 });137 });138 </script>139 <div role="note" aria-label="source link">140 <h3>This page</h3>141 <ul class="this-page-menu">142 <li><a href="../bugs.html">Report a bug</a></li>143 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>144 <li>145 <a href="https://github.com/python/cpython/blob/main/Doc/library/reprlib.rst?plain=1"146 rel="nofollow">Show source147 </a>148 </li>149 150 </ul>151 </div>152 </nav>153 </div>154</div>155 156 157 <div class="related" role="navigation" aria-label="Related">158 <h3>Navigation</h3>159 <ul>160 <li class="right" style="margin-right: 10px">161 <a href="../genindex.html" title="General Index"162 accesskey="I">index</a></li>163 <li class="right" >164 <a href="../py-modindex.html" title="Python Module Index"165 >modules</a> |</li>166 <li class="right" >167 <a href="enum.html" title="enum — Support for enumerations"168 accesskey="N">next</a> |</li>169 <li class="right" >170 <a href="pprint.html" title="pprint — Data pretty printer"171 accesskey="P">previous</a> |</li>172 173 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>174 <li><a href="https://www.python.org/">Python</a> »</li>175 <li class="switchers">176 <div class="language_switcher_placeholder"></div>177 <div class="version_switcher_placeholder"></div>178 </li>179 <li>180 181 </li>182 <li id="cpython-language-and-version">183 <a href="../index.html">3.15.0a6 Documentation</a> »184 </li>185 186 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>187 <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">Data Types</a> »</li>188 <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>189 <li class="right">190 191 192 <div class="inline-search" role="search">193 <form class="inline-search" action="../search.html" method="get">194 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">195 <input type="submit" value="Go">196 </form>197 </div>198 |199 </li>200 <li class="right">201<label class="theme-selector-label">202 Theme203 <select class="theme-selector" oninput="activateTheme(this.value)">204 <option value="auto" selected>Auto</option>205 <option value="light">Light</option>206 <option value="dark">Dark</option>207 </select>208</label> |</li>209 210 </ul>211 </div> 212 213 <div class="document">214 <div class="documentwrapper">215 <div class="bodywrapper">216 <div class="body" role="main">217 218 <section id="module-reprlib">219<span id="reprlib-alternate-repr-implementation"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> — 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>220<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/reprlib.py">Lib/reprlib.py</a></p>221<hr class="docutils" />222<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code> module provides a means for producing object representations223with limits on the size of the resulting strings. This is used in the Python224debugger and may be useful in other contexts as well.</p>225<p>This module provides a class, an instance, and a function:</p>226<dl class="py class">227<dt class="sig sig-object py" id="reprlib.Repr">228<em class="property"><span class="k"><span class="pre">class</span></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="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></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>229<dd><p>Class which provides formatting services useful in implementing functions230similar 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 types231are added to avoid the generation of representations which are excessively long.</p>232<p>The keyword arguments of the constructor can be used as a shortcut to set the233attributes of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Repr</span></code> instance. Which means that the following234initialization:</p>235<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>236</pre></div>237</div>238<p>Is equivalent to:</p>239<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>240<span class="n">aRepr</span><span class="o">.</span><span class="n">maxlevel</span> <span class="o">=</span> <span class="mi">3</span>241</pre></div>242</div>243<p>See section <a class="reference internal" href="#id1">Repr Objects</a> for more information about <code class="xref py py-class docutils literal notranslate"><span class="pre">Repr</span></code>244attributes.</p>245<div class="versionchanged">246<p><span class="versionmodified changed">Changed in version 3.12: </span>Allow attributes to be set via keyword arguments.</p>247</div>248</dd></dl>249 250<dl class="py data">251<dt class="sig sig-object py" id="reprlib.aRepr">252<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>253<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 the254<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 this255object will affect the size limits used by <code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code> and the Python256debugger.</p>257</dd></dl>258 259<dl class="py function">260<dt class="sig sig-object py" id="reprlib.repr">261<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>262<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 string263similar to that returned by the built-in function of the same name, but with264limits on most sizes.</p>265</dd></dl>266 267<p>In addition to size-limiting tools, the module also provides a decorator for268detecting 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 a269placeholder string instead.</p>270<dl class="py function" id="index-0">271<dt class="sig sig-object py" id="reprlib.recursive_repr">272<span class="sig-prename descclassname"><span class="pre">@</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>273<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 the274same thread. If a recursive call is made, the <em>fillvalue</em> is returned,275otherwise, the usual <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> call is made. For example:</p>276<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">reprlib</span><span class="w"> </span><span class="kn">import</span> <span class="n">recursive_repr</span>277<span class="gp">>>> </span><span class="k">class</span><span class="w"> </span><span class="nc">MyList</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>278<span class="gp">... </span> <span class="nd">@recursive_repr</span><span class="p">()</span>279<span class="gp">... </span> <span class="k">def</span><span class="w"> </span><span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>280<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>281<span class="gp">...</span>282<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>283<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>284<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>285<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>286<span class="go"><'a'|'b'|'c'|...|'x'></span>287</pre></div>288</div>289<div class="versionadded">290<p><span class="versionmodified added">Added in version 3.2.</span></p>291</div>292</dd></dl>293 294<section id="repr-objects">295<span id="id1"></span><h2>Repr Objects<a class="headerlink" href="#repr-objects" title="Link to this heading">¶</a></h2>296<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 provide297size limits for the representations of different object types, and methods298which format specific object types.</p>299<dl class="py attribute">300<dt class="sig sig-object py" id="reprlib.Repr.fillvalue">301<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>302<dd><p>This string is displayed for recursive references. It defaults to303<code class="docutils literal notranslate"><span class="pre">...</span></code>.</p>304<div class="versionadded">305<p><span class="versionmodified added">Added in version 3.11.</span></p>306</div>307</dd></dl>308 309<dl class="py attribute">310<dt class="sig sig-object py" id="reprlib.Repr.maxlevel">311<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>312<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>313</dd></dl>314 315<dl class="py attribute">316<dt class="sig sig-object py" id="reprlib.Repr.maxdict">317<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>318<dt class="sig sig-object py" id="reprlib.Repr.maxlist">319<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>320<dt class="sig sig-object py" id="reprlib.Repr.maxtuple">321<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>322<dt class="sig sig-object py" id="reprlib.Repr.maxset">323<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>324<dt class="sig sig-object py" id="reprlib.Repr.maxfrozenset">325<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>326<dt class="sig sig-object py" id="reprlib.Repr.maxdeque">327<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>328<dt class="sig sig-object py" id="reprlib.Repr.maxarray">329<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>330<dd><p>Limits on the number of entries represented for the named object type. The331default 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> for332the others.</p>333</dd></dl>334 335<dl class="py attribute">336<dt class="sig sig-object py" id="reprlib.Repr.maxlong">337<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>338<dd><p>Maximum number of characters in the representation for an integer. Digits339are dropped from the middle. The default is <code class="docutils literal notranslate"><span class="pre">40</span></code>.</p>340</dd></dl>341 342<dl class="py attribute">343<dt class="sig sig-object py" id="reprlib.Repr.maxstring">344<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>345<dd><p>Limit on the number of characters in the representation of the string. Note346that the “normal” representation of the string is used as the character source:347if escape sequences are needed in the representation, these may be mangled when348the representation is shortened. The default is <code class="docutils literal notranslate"><span class="pre">30</span></code>.</p>349</dd></dl>350 351<dl class="py attribute">352<dt class="sig sig-object py" id="reprlib.Repr.maxother">353<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>354<dd><p>This limit is used to control the size of object types for which no specific355formatting 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 a356similar 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>357</dd></dl>358 359<dl class="py attribute">360<dt class="sig sig-object py" id="reprlib.Repr.indent">361<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>362<dd><p>If this attribute is set to <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default), the output is formatted363with 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>.364For example:</p>365<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">example</span> <span class="o">=</span> <span class="p">[</span>366<span class="gp">... </span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'spam'</span><span class="p">,</span> <span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">:</span> <span class="s1">'spam eggs'</span><span class="p">,</span> <span class="s1">'c'</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">'ham'</span><span class="p">]</span>367<span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">reprlib</span>368<span class="gp">>>> </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>369<span class="gp">>>> </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>370<span class="go">[1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham']</span>371</pre></div>372</div>373<p>If <code class="xref py py-attr docutils literal notranslate"><span class="pre">indent</span></code> is set to a string, each recursion level374is placed on its own line, indented by that string:</p>375<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">aRepr</span><span class="o">.</span><span class="n">indent</span> <span class="o">=</span> <span class="s1">'-->'</span>376<span class="gp">>>> </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>377<span class="go">[</span>378<span class="go">-->1,</span>379<span class="go">-->'spam',</span>380<span class="go">-->{</span>381<span class="go">-->-->'a': 2,</span>382<span class="go">-->-->'b': 'spam eggs',</span>383<span class="go">-->-->'c': {</span>384<span class="go">-->-->-->3: 4.5,</span>385<span class="go">-->-->-->6: [],</span>386<span class="go">-->-->},</span>387<span class="go">-->},</span>388<span class="go">-->'ham',</span>389<span class="go">]</span>390</pre></div>391</div>392<p>Setting <code class="xref py py-attr docutils literal notranslate"><span class="pre">indent</span></code> to a positive integer value behaves as if it393was set to a string with that number of spaces:</p>394<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">aRepr</span><span class="o">.</span><span class="n">indent</span> <span class="o">=</span> <span class="mi">4</span>395<span class="gp">>>> </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>396<span class="go">[</span>397<span class="go"> 1,</span>398<span class="go"> 'spam',</span>399<span class="go"> {</span>400<span class="go"> 'a': 2,</span>401<span class="go"> 'b': 'spam eggs',</span>402<span class="go"> 'c': {</span>403<span class="go"> 3: 4.5,</span>404<span class="go"> 6: [],</span>405<span class="go"> },</span>406<span class="go"> },</span>407<span class="go"> 'ham',</span>408<span class="go">]</span>409</pre></div>410</div>411<div class="versionadded">412<p><span class="versionmodified added">Added in version 3.12.</span></p>413</div>414</dd></dl>415 416<dl class="py method">417<dt class="sig sig-object py" id="reprlib.Repr.repr">418<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>419<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 by420the instance.</p>421</dd></dl>422 423<dl class="py method">424<dt class="sig sig-object py" id="reprlib.Repr.repr1">425<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>426<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> to427determine which formatting method to call, passing it <em>obj</em> and <em>level</em>. The428type-specific methods should call <code class="xref py py-meth docutils literal notranslate"><span class="pre">repr1()</span></code> to perform recursive formatting,429with <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>430</dd></dl>431 432<dl class="py method">433<dt class="sig sig-object py">434<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>435<dd><p>Formatting methods for specific types are implemented as methods with a name436based on the type name. In the method name, <strong>TYPE</strong> is replaced by437<code class="docutils literal notranslate"><span class="pre">'_'.join(type(obj).__name__.split())</span></code>. Dispatch to these methods is438handled 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 recursively439format 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>440</dd></dl>441 442</section>443<section id="subclassing-repr-objects">444<span id="subclassing-reprs"></span><h2>Subclassing Repr Objects<a class="headerlink" href="#subclassing-repr-objects" title="Link to this heading">¶</a></h2>445<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 of446<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 modify447the handling of types already supported. This example shows how special support448for file objects could be added:</p>449<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">reprlib</span>450<span class="kn">import</span><span class="w"> </span><span class="nn">sys</span>451 452<span class="k">class</span><span class="w"> </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>453 454 <span class="k">def</span><span class="w"> </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>455 <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>456 <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">name</span>457 <span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>458 459<span class="n">aRepr</span> <span class="o">=</span> <span class="n">MyRepr</span><span class="p">()</span>460<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>461</pre></div>462</div>463<div class="highlight-none notranslate"><div class="highlight"><pre><span></span><stdin>464</pre></div>465</div>466</section>467</section>468 469 470 <div class="clearer"></div>471 </div>472 </div>473 </div>474 <div class="sphinxsidebar" role="navigation" aria-label="Main">475 <div class="sphinxsidebarwrapper">476 <div>477 <h3><a href="../contents.html">Table of Contents</a></h3>478 <ul>479<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>480<li><a class="reference internal" href="#repr-objects">Repr Objects</a></li>481<li><a class="reference internal" href="#subclassing-repr-objects">Subclassing Repr Objects</a></li>482</ul>483</li>484</ul>485 486 </div>487 <div>488 <h4>Previous topic</h4>489 <p class="topless"><a href="pprint.html"490 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code> — Data pretty printer</a></p>491 </div>492 <div>493 <h4>Next topic</h4>494 <p class="topless"><a href="enum.html"495 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">enum</span></code> — Support for enumerations</a></p>496 </div>497 <script>498 document.addEventListener('DOMContentLoaded', () => {499 const title = document.querySelector('meta[property="og:title"]').content;500 const elements = document.querySelectorAll('.improvepage');501 const pageurl = window.location.href.split('?')[0];502 elements.forEach(element => {503 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));504 url.searchParams.set('pagetitle', title);505 url.searchParams.set('pageurl', pageurl);506 url.searchParams.set('pagesource', "library/reprlib.rst");507 element.href = url.toString();508 });509 });510 </script>511 <div role="note" aria-label="source link">512 <h3>This page</h3>513 <ul class="this-page-menu">514 <li><a href="../bugs.html">Report a bug</a></li>515 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>516 <li>517 <a href="https://github.com/python/cpython/blob/main/Doc/library/reprlib.rst?plain=1"518 rel="nofollow">Show source519 </a>520 </li>521 522 </ul>523 </div>524 </div>525<div id="sidebarbutton" title="Collapse sidebar">526<span>«</span>527</div>528 529 </div>530 <div class="clearer"></div>531 </div> 532 <div class="related" role="navigation" aria-label="Related">533 <h3>Navigation</h3>534 <ul>535 <li class="right" style="margin-right: 10px">536 <a href="../genindex.html" title="General Index"537 >index</a></li>538 <li class="right" >539 <a href="../py-modindex.html" title="Python Module Index"540 >modules</a> |</li>541 <li class="right" >542 <a href="enum.html" title="enum — Support for enumerations"543 >next</a> |</li>544 <li class="right" >545 <a href="pprint.html" title="pprint — Data pretty printer"546 >previous</a> |</li>547 548 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>549 <li><a href="https://www.python.org/">Python</a> »</li>550 <li class="switchers">551 <div class="language_switcher_placeholder"></div>552 <div class="version_switcher_placeholder"></div>553 </li>554 <li>555 556 </li>557 <li id="cpython-language-and-version">558 <a href="../index.html">3.15.0a6 Documentation</a> »559 </li>560 561 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>562 <li class="nav-item nav-item-2"><a href="datatypes.html" >Data Types</a> »</li>563 <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>564 <li class="right">565 566 567 <div class="inline-search" role="search">568 <form class="inline-search" action="../search.html" method="get">569 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">570 <input type="submit" value="Go">571 </form>572 </div>573 |574 </li>575 <li class="right">576<label class="theme-selector-label">577 Theme578 <select class="theme-selector" oninput="activateTheme(this.value)">579 <option value="auto" selected>Auto</option>580 <option value="light">Light</option>581 <option value="dark">Dark</option>582 </select>583</label> |</li>584 585 </ul>586 </div> 587 <div class="footer">588 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.589 <br>590 This page is licensed under the Python Software Foundation License Version 2.591 <br>592 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.593 <br>594 595 See <a href="/license.html">History and License</a> for more information.<br>596 597 598 <br>599 600 The Python Software Foundation is a non-profit corporation.601<a href="https://www.python.org/psf/donations/">Please donate.</a>602<br>603 <br>604 Last updated on Mar 10, 2026 (08:58 UTC).605 606 <a href="/bugs.html">Found a bug</a>?607 608 <br>609 610 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.611 </div>612 613 </body>614</html>