Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 781

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!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>7.13. reprlib — Alternate repr() implementation &mdash; Python v3.2.2 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:     '3.2.2',
        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 v3.2.2 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 v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="7. Data Types" href="datatypes.html" />
    <link rel="next" title="8. Numeric and Mathematical Modules" href="numeric.html" />
    <link rel="prev" title="7.12. pprint — Data pretty printer" href="pprint.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </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="8. Numeric and Mathematical Modules"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="pprint.html" title="7.12. 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="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" accesskey="U">7. Data Types</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-reprlib">
<span id="reprlib-alternate-repr-implementation"></span><h1>7.13. <a class="reference internal" href="#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><tt class="xref py py-mod docutils literal"><span class="pre">reprlib</span></tt></a> &#8212; Alternate <a class="reference internal" href="functions.html#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> implementation<a class="headerlink" href="#module-reprlib" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/3.2/Lib/reprlib.py">Lib/reprlib.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><tt class="xref py py-mod docutils literal"><span class="pre">reprlib</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="reprlib.Repr">
<em class="property">class </em><tt class="descclassname">reprlib.</tt><tt class="descname">Repr</tt><a class="headerlink" href="#reprlib.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="functions.html#repr" title="repr"><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="reprlib.aRepr">
<tt class="descclassname">reprlib.</tt><tt class="descname">aRepr</tt><a class="headerlink" href="#reprlib.aRepr" title="Permalink to this definition">¶</a></dt>
<dd><p>This is an instance of <a class="reference internal" href="#reprlib.Repr" title="reprlib.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="#reprlib.repr" title="reprlib.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="#reprlib.repr" title="reprlib.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="reprlib.repr">
<tt class="descclassname">reprlib.</tt><tt class="descname">repr</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#reprlib.repr" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the <a class="reference internal" href="#reprlib.Repr.repr" title="reprlib.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>

<p>In addition to size-limiting tools, the module also provides a decorator for
detecting recursive calls to <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> and substituting a placeholder
string instead.</p>
<dl class="function">
<dt id="reprlib.recursive_repr">
<tt class="descclassname">&#64;</tt><tt class="descclassname">reprlib.</tt><tt class="descname">recursive_repr</tt><big>(</big><em>fillvalue=&quot;...&quot;</em><big>)</big><a class="headerlink" href="#reprlib.recursive_repr" title="Permalink to this definition">¶</a></dt>
<dd><p>Decorator for <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> methods to detect recursive calls within the
same thread.  If a recursive call is made, the <em>fillvalue</em> is returned,
otherwise, the usual <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> call is made.  For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><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>
<span class="gp">... </span>    <span class="nd">@recursive_repr</span><span class="p">()</span>
<span class="gp">... </span>     <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>         <span class="k">return</span> <span class="s">&#39;&lt;&#39;</span> <span class="o">+</span> <span class="s">&#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="s">&#39;&gt;&#39;</span>
<span class="gp">...</span>
<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="s">&#39;abc&#39;</span><span class="p">)</span>
<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>
<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="s">&#39;x&#39;</span><span class="p">)</span>
<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>
<span class="go">&lt;&#39;a&#39;|&#39;b&#39;|&#39;c&#39;|...|&#39;x&#39;&gt;</span>
</pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 3.2.</span></p>
</dd></dl>

<div class="section" id="repr-objects">
<span id="id1"></span><h2>7.13.1. Repr Objects<a class="headerlink" href="#repr-objects" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#reprlib.Repr" title="reprlib.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="reprlib.Repr.maxlevel">
<tt class="descclassname">Repr.</tt><tt class="descname">maxlevel</tt><a class="headerlink" href="#reprlib.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="reprlib.Repr.maxdict">
<tt class="descclassname">Repr.</tt><tt class="descname">maxdict</tt><a class="headerlink" href="#reprlib.Repr.maxdict" title="Permalink to this definition">¶</a></dt>
<dt id="reprlib.Repr.maxlist">
<tt class="descclassname">Repr.</tt><tt class="descname">maxlist</tt><a class="headerlink" href="#reprlib.Repr.maxlist" title="Permalink to this definition">¶</a></dt>
<dt id="reprlib.Repr.maxtuple">
<tt class="descclassname">Repr.</tt><tt class="descname">maxtuple</tt><a class="headerlink" href="#reprlib.Repr.maxtuple" title="Permalink to this definition">¶</a></dt>
<dt id="reprlib.Repr.maxset">
<tt class="descclassname">Repr.</tt><tt class="descname">maxset</tt><a class="headerlink" href="#reprlib.Repr.maxset" title="Permalink to this definition">¶</a></dt>
<dt id="reprlib.Repr.maxfrozenset">
<tt class="descclassname">Repr.</tt><tt class="descname">maxfrozenset</tt><a class="headerlink" href="#reprlib.Repr.maxfrozenset" title="Permalink to this definition">¶</a></dt>
<dt id="reprlib.Repr.maxdeque">
<tt class="descclassname">Repr.</tt><tt class="descname">maxdeque</tt><a class="headerlink" href="#reprlib.Repr.maxdeque" title="Permalink to this definition">¶</a></dt>
<dt id="reprlib.Repr.maxarray">
<tt class="descclassname">Repr.</tt><tt class="descname">maxarray</tt><a class="headerlink" href="#reprlib.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="#reprlib.Repr.maxdict" title="reprlib.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="#reprlib.Repr.maxarray" title="reprlib.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>
</dd></dl>

<dl class="attribute">
<dt id="reprlib.Repr.maxlong">
<tt class="descclassname">Repr.</tt><tt class="descname">maxlong</tt><a class="headerlink" href="#reprlib.Repr.maxlong" title="Permalink to this definition">¶</a></dt>
<dd><p>Maximum number of characters in the representation for an 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="reprlib.Repr.maxstring">
<tt class="descclassname">Repr.</tt><tt class="descname">maxstring</tt><a class="headerlink" href="#reprlib.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 &#8220;normal&#8221; 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="reprlib.Repr.maxother">
<tt class="descclassname">Repr.</tt><tt class="descname">maxother</tt><a class="headerlink" href="#reprlib.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="#reprlib.Repr" title="reprlib.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="#reprlib.Repr.maxstring" title="reprlib.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="reprlib.Repr.repr">
<tt class="descclassname">Repr.</tt><tt class="descname">repr</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#reprlib.Repr.repr" title="Permalink to this definition">¶</a></dt>
<dd><p>The equivalent to the built-in <a class="reference internal" href="functions.html#repr" title="repr"><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="reprlib.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="#reprlib.Repr.repr1" title="Permalink to this definition">¶</a></dt>
<dd><p>Recursive implementation used by <a class="reference internal" href="#reprlib.Repr.repr" title="reprlib.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="#reprlib.Repr.repr1" title="reprlib.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>7.13.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="#reprlib.Repr.repr1" title="reprlib.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="#reprlib.Repr" title="reprlib.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-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">reprlib</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="k">class</span> <span class="nc">MyRepr</span><span class="p">(</span><span class="n">reprlib</span><span class="o">.</span><span class="n">Repr</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">repr_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">&#39;&lt;stdin&gt;&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;stdout&gt;&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;stderr&gt;&#39;</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="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="c"># prints &#39;&lt;stdin&gt;&#39;</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="#">7.13. <tt class="docutils literal"><span class="pre">reprlib</span></tt> &#8212; Alternate <tt class="docutils literal"><span class="pre">repr()</span></tt> implementation</a><ul>
<li><a class="reference internal" href="#repr-objects">7.13.1. Repr Objects</a></li>
<li><a class="reference internal" href="#subclassing-repr-objects">7.13.2. Subclassing Repr Objects</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="pprint.html"
                        title="previous chapter">7.12. <tt class="docutils literal docutils literal docutils literal"><span class="pre">pprint</span></tt> &#8212; Data pretty printer</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numeric.html"
                        title="next chapter">8. 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/reprlib.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" size="18" />
      <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="8. Numeric and Mathematical Modules"
             >next</a> |</li>
        <li class="right" >
          <a href="pprint.html" title="7.12. 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="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="datatypes.html" >7. Data Types</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, 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 Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>