GIF89a;
Direktori : /usr/share/doc/python-docs-2.7.5/html/library/ |
Current File : //usr/share/doc/python-docs-2.7.5/html/library/repr.html |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>8.19. repr — Alternate repr() implementation — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="8. Data Types" href="datatypes.html" /> <link rel="next" title="9. Numeric and Mathematical Modules" href="numeric.html" /> <link rel="prev" title="8.18. pprint — Data pretty printer" href="pprint.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="numeric.html" title="9. Numeric and Mathematical Modules" accesskey="N">next</a> |</li> <li class="right" > <a href="pprint.html" title="8.18. pprint — Data pretty printer" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-repr"> <span id="repr-alternate-repr-implementation"></span><h1>8.19. <a class="reference internal" href="#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-mod docutils literal"><span class="pre">repr</span></tt></a> — Alternate <a class="reference internal" href="#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> implementation<a class="headerlink" href="#module-repr" title="Permalink to this headline">¶</a></h1> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <a class="reference internal" href="#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-mod docutils literal"><span class="pre">repr</span></tt></a> module has been renamed to <tt class="xref py py-mod docutils literal"><span class="pre">reprlib</span></tt> in Python 3. The <a class="reference internal" href="../glossary.html#term-to3"><em class="xref std std-term">2to3</em></a> tool will automatically adapt imports when converting your sources to Python 3.</p> </div> <p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/repr.py">Lib/repr.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-mod docutils literal"><span class="pre">repr</span></tt></a> 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 useful in other contexts as well.</p> <p>This module provides a class, an instance, and a function:</p> <dl class="class"> <dt id="repr.Repr"> <em class="property">class </em><tt class="descclassname">repr.</tt><tt class="descname">Repr</tt><a class="headerlink" href="#repr.Repr" title="Permalink to this definition">¶</a></dt> <dd><p>Class which provides formatting services useful in implementing functions similar to the built-in <a class="reference internal" href="#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a>; size limits for different object types are added to avoid the generation of representations which are excessively long.</p> </dd></dl> <dl class="data"> <dt id="repr.aRepr"> <tt class="descclassname">repr.</tt><tt class="descname">aRepr</tt><a class="headerlink" href="#repr.aRepr" title="Permalink to this definition">¶</a></dt> <dd><p>This is an instance of <a class="reference internal" href="#repr.Repr" title="repr.Repr"><tt class="xref py py-class docutils literal"><span class="pre">Repr</span></tt></a> which is used to provide the <a class="reference internal" href="#repr.repr" title="repr.repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> function described below. Changing the attributes of this object will affect the size limits used by <a class="reference internal" href="#repr.repr" title="repr.repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> and the Python debugger.</p> </dd></dl> <dl class="function"> <dt id="repr.repr"> <tt class="descclassname">repr.</tt><tt class="descname">repr</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#repr.repr" title="Permalink to this definition">¶</a></dt> <dd><p>This is the <a class="reference internal" href="#repr.Repr.repr" title="repr.Repr.repr"><tt class="xref py py-meth docutils literal"><span class="pre">repr()</span></tt></a> method of <tt class="docutils literal"><span class="pre">aRepr</span></tt>. It returns a string similar to that returned by the built-in function of the same name, but with limits on most sizes.</p> </dd></dl> <div class="section" id="repr-objects"> <span id="id1"></span><h2>8.19.1. Repr Objects<a class="headerlink" href="#repr-objects" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="#repr.Repr" title="repr.Repr"><tt class="xref py py-class docutils literal"><span class="pre">Repr</span></tt></a> instances provide several attributes which can be used to provide size limits for the representations of different object types, and methods which format specific object types.</p> <dl class="attribute"> <dt id="repr.Repr.maxlevel"> <tt class="descclassname">Repr.</tt><tt class="descname">maxlevel</tt><a class="headerlink" href="#repr.Repr.maxlevel" title="Permalink to this definition">¶</a></dt> <dd><p>Depth limit on the creation of recursive representations. The default is <tt class="docutils literal"><span class="pre">6</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="repr.Repr.maxdict"> <tt class="descclassname">Repr.</tt><tt class="descname">maxdict</tt><a class="headerlink" href="#repr.Repr.maxdict" title="Permalink to this definition">¶</a></dt> <dt id="repr.Repr.maxlist"> <tt class="descclassname">Repr.</tt><tt class="descname">maxlist</tt><a class="headerlink" href="#repr.Repr.maxlist" title="Permalink to this definition">¶</a></dt> <dt id="repr.Repr.maxtuple"> <tt class="descclassname">Repr.</tt><tt class="descname">maxtuple</tt><a class="headerlink" href="#repr.Repr.maxtuple" title="Permalink to this definition">¶</a></dt> <dt id="repr.Repr.maxset"> <tt class="descclassname">Repr.</tt><tt class="descname">maxset</tt><a class="headerlink" href="#repr.Repr.maxset" title="Permalink to this definition">¶</a></dt> <dt id="repr.Repr.maxfrozenset"> <tt class="descclassname">Repr.</tt><tt class="descname">maxfrozenset</tt><a class="headerlink" href="#repr.Repr.maxfrozenset" title="Permalink to this definition">¶</a></dt> <dt id="repr.Repr.maxdeque"> <tt class="descclassname">Repr.</tt><tt class="descname">maxdeque</tt><a class="headerlink" href="#repr.Repr.maxdeque" title="Permalink to this definition">¶</a></dt> <dt id="repr.Repr.maxarray"> <tt class="descclassname">Repr.</tt><tt class="descname">maxarray</tt><a class="headerlink" href="#repr.Repr.maxarray" title="Permalink to this definition">¶</a></dt> <dd><p>Limits on the number of entries represented for the named object type. The default is <tt class="docutils literal"><span class="pre">4</span></tt> for <a class="reference internal" href="#repr.Repr.maxdict" title="repr.Repr.maxdict"><tt class="xref py py-attr docutils literal"><span class="pre">maxdict</span></tt></a>, <tt class="docutils literal"><span class="pre">5</span></tt> for <a class="reference internal" href="#repr.Repr.maxarray" title="repr.Repr.maxarray"><tt class="xref py py-attr docutils literal"><span class="pre">maxarray</span></tt></a>, and <tt class="docutils literal"><span class="pre">6</span></tt> for the others.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.4: </span><a class="reference internal" href="#repr.Repr.maxset" title="repr.Repr.maxset"><tt class="xref py py-attr docutils literal"><span class="pre">maxset</span></tt></a>, <a class="reference internal" href="#repr.Repr.maxfrozenset" title="repr.Repr.maxfrozenset"><tt class="xref py py-attr docutils literal"><span class="pre">maxfrozenset</span></tt></a>, and <a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-attr docutils literal"><span class="pre">set</span></tt></a>.</p> </dd></dl> <dl class="attribute"> <dt id="repr.Repr.maxlong"> <tt class="descclassname">Repr.</tt><tt class="descname">maxlong</tt><a class="headerlink" href="#repr.Repr.maxlong" title="Permalink to this definition">¶</a></dt> <dd><p>Maximum number of characters in the representation for a long integer. Digits are dropped from the middle. The default is <tt class="docutils literal"><span class="pre">40</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="repr.Repr.maxstring"> <tt class="descclassname">Repr.</tt><tt class="descname">maxstring</tt><a class="headerlink" href="#repr.Repr.maxstring" title="Permalink to this definition">¶</a></dt> <dd><p>Limit on the number of characters in the representation of the string. Note that the “normal” representation of the string is used as the character source: if escape sequences are needed in the representation, these may be mangled when the representation is shortened. The default is <tt class="docutils literal"><span class="pre">30</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="repr.Repr.maxother"> <tt class="descclassname">Repr.</tt><tt class="descname">maxother</tt><a class="headerlink" href="#repr.Repr.maxother" title="Permalink to this definition">¶</a></dt> <dd><p>This limit is used to control the size of object types for which no specific formatting method is available on the <a class="reference internal" href="#repr.Repr" title="repr.Repr"><tt class="xref py py-class docutils literal"><span class="pre">Repr</span></tt></a> object. It is applied in a similar manner as <a class="reference internal" href="#repr.Repr.maxstring" title="repr.Repr.maxstring"><tt class="xref py py-attr docutils literal"><span class="pre">maxstring</span></tt></a>. The default is <tt class="docutils literal"><span class="pre">20</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="repr.Repr.repr"> <tt class="descclassname">Repr.</tt><tt class="descname">repr</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#repr.Repr.repr" title="Permalink to this definition">¶</a></dt> <dd><p>The equivalent to the built-in <a class="reference internal" href="#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> that uses the formatting imposed by the instance.</p> </dd></dl> <dl class="method"> <dt id="repr.Repr.repr1"> <tt class="descclassname">Repr.</tt><tt class="descname">repr1</tt><big>(</big><em>obj</em>, <em>level</em><big>)</big><a class="headerlink" href="#repr.Repr.repr1" title="Permalink to this definition">¶</a></dt> <dd><p>Recursive implementation used by <a class="reference internal" href="#repr.Repr.repr" title="repr.Repr.repr"><tt class="xref py py-meth docutils literal"><span class="pre">repr()</span></tt></a>. This uses the type of <em>obj</em> to determine which formatting method to call, passing it <em>obj</em> and <em>level</em>. The type-specific methods should call <a class="reference internal" href="#repr.Repr.repr1" title="repr.Repr.repr1"><tt class="xref py py-meth docutils literal"><span class="pre">repr1()</span></tt></a> to perform recursive formatting, with <tt class="docutils literal"><span class="pre">level</span> <span class="pre">-</span> <span class="pre">1</span></tt> for the value of <em>level</em> in the recursive call.</p> </dd></dl> <dl class="method"> <dt> <tt class="descclassname">Repr.</tt><tt class="descname">repr_TYPE</tt><big>(</big><em>obj</em>, <em>level</em><big>)</big></dt> <dd><p>Formatting methods for specific types are implemented as methods with a name based on the type name. In the method name, <strong>TYPE</strong> is replaced by <tt class="docutils literal"><span class="pre">string.join(string.split(type(obj).__name__,</span> <span class="pre">'_'))</span></tt>. Dispatch to these methods is handled by <tt class="xref py py-meth docutils literal"><span class="pre">repr1()</span></tt>. Type-specific methods which need to recursively format a value should call <tt class="docutils literal"><span class="pre">self.repr1(subobj,</span> <span class="pre">level</span> <span class="pre">-</span> <span class="pre">1)</span></tt>.</p> </dd></dl> </div> <div class="section" id="subclassing-repr-objects"> <span id="subclassing-reprs"></span><h2>8.19.2. Subclassing Repr Objects<a class="headerlink" href="#subclassing-repr-objects" title="Permalink to this headline">¶</a></h2> <p>The use of dynamic dispatching by <a class="reference internal" href="#repr.Repr.repr1" title="repr.Repr.repr1"><tt class="xref py py-meth docutils literal"><span class="pre">Repr.repr1()</span></tt></a> allows subclasses of <a class="reference internal" href="#repr.Repr" title="repr.Repr"><tt class="xref py py-class docutils literal"><span class="pre">Repr</span></tt></a> to add support for additional built-in object types or to modify the handling of types already supported. This example shows how special support for file objects could be added:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">repr</span> <span class="kn">as</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_file</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="s">'<stdin>'</span><span class="p">,</span> <span class="s">'<stdout>'</span><span class="p">,</span> <span class="s">'<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">else</span><span class="p">:</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="k">print</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="c"># prints '<stdin>'</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">8.19. <tt class="docutils literal"><span class="pre">repr</span></tt> — Alternate <tt class="docutils literal"><span class="pre">repr()</span></tt> implementation</a><ul> <li><a class="reference internal" href="#repr-objects">8.19.1. Repr Objects</a></li> <li><a class="reference internal" href="#subclassing-repr-objects">8.19.2. Subclassing Repr Objects</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="pprint.html" title="previous chapter">8.18. <tt class="docutils literal"><span class="pre">pprint</span></tt> — Data pretty printer</a></p> <h4>Next topic</h4> <p class="topless"><a href="numeric.html" title="next chapter">9. Numeric and Mathematical Modules</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/repr.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="numeric.html" title="9. Numeric and Mathematical Modules" >next</a> |</li> <li class="right" > <a href="pprint.html" title="8.18. pprint — Data pretty printer" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="datatypes.html" >8. Data Types</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>