<!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 v2.6.6 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.6.6',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v2.6.6 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 v2.6.6 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" />
</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="../modindex.html" title="Global Module Index"
accesskey="M">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="../index.html">Python v2.6.6 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">
<h1>8.19. <tt class="xref docutils literal"><span class="pre">repr</span></tt> — Alternate <a title="repr" class="reference external" href="functions.html#repr"><tt class="xref 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 <tt class="xref docutils literal"><span class="pre">repr</span></tt> module has been renamed to <tt class="xref docutils literal"><span class="pre">reprlib</span></tt> in Python 3.0. The
<a class="reference external" href="../glossary.html#term-to3"><em class="xref">2to3</em></a> tool will automatically adapt imports when converting your
sources to 3.0.</p>
</div>
<p>The <tt class="xref docutils literal"><span class="pre">repr</span></tt> 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>Class which provides formatting services useful in implementing functions
similar to the built-in <a title="repr" class="reference external" href="functions.html#repr"><tt class="xref 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.</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>This is an instance of <a title="repr.Repr" class="reference internal" href="#repr.Repr"><tt class="xref docutils literal"><span class="pre">Repr</span></tt></a> which is used to provide the <a title="repr.repr" class="reference internal" href="#repr.repr"><tt class="xref 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 title="repr.repr" class="reference internal" href="#repr.repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a> and the Python debugger.</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>This is the <a title="repr.Repr.repr" class="reference internal" href="#repr.Repr.repr"><tt class="xref 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.</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 title="repr.Repr" class="reference internal" href="#repr.Repr"><tt class="xref docutils literal"><span class="pre">Repr</span></tt></a> instances provide several members 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>Depth limit on the creation of recursive representations. The default is <tt class="docutils literal"><span class="pre">6</span></tt>.</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 title="repr.Repr.maxdict" class="reference internal" href="#repr.Repr.maxdict"><tt class="xref docutils literal"><span class="pre">maxdict</span></tt></a>, <tt class="docutils literal"><span class="pre">5</span></tt> for <a title="repr.Repr.maxarray" class="reference internal" href="#repr.Repr.maxarray"><tt class="xref 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 title="repr.Repr.maxset" class="reference internal" href="#repr.Repr.maxset"><tt class="xref docutils literal"><span class="pre">maxset</span></tt></a>, <a title="repr.Repr.maxfrozenset" class="reference internal" href="#repr.Repr.maxfrozenset"><tt class="xref docutils literal"><span class="pre">maxfrozenset</span></tt></a>, and <a title="set" class="reference external" href="stdtypes.html#set"><tt class="xref 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>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>.</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>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>.</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>This limit is used to control the size of object types for which no specific
formatting method is available on the <a title="repr.Repr" class="reference internal" href="#repr.Repr"><tt class="xref docutils literal"><span class="pre">Repr</span></tt></a> object. It is applied in a
similar manner as <a title="repr.Repr.maxstring" class="reference internal" href="#repr.Repr.maxstring"><tt class="xref docutils literal"><span class="pre">maxstring</span></tt></a>. The default is <tt class="docutils literal"><span class="pre">20</span></tt>.</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>The equivalent to the built-in <a title="repr" class="reference external" href="functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a> that uses the formatting imposed by
the instance.</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>Recursive implementation used by <a title="repr.Repr.repr" class="reference internal" href="#repr.Repr.repr"><tt class="xref 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 title="repr.Repr.repr1" class="reference internal" href="#repr.Repr.repr1"><tt class="xref 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.</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>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 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>.</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 title="repr.Repr.repr1" class="reference internal" href="#repr.Repr.repr1"><tt class="xref docutils literal"><span class="pre">Repr.repr1()</span></tt></a> allows subclasses of
<a title="repr.Repr" class="reference internal" href="#repr.Repr"><tt class="xref 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 external" 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 external" href="#repr-objects">8.19.1. Repr Objects</a></li>
<li><a class="reference external" 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 docutils literal 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" 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="../modindex.html" title="Global 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="../index.html">Python v2.6.6 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-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 Jul 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez