1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>8.19. repr — Alternate repr() implementation — Python 2.7.11 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.11 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
33
<link rel="up" title="8. Data Types" href="datatypes.html" />
34
<link rel="next" title="9. Numeric and Mathematical Modules" href="numeric.html" />
35
<link rel="prev" title="8.18. pprint — Data pretty printer" href="pprint.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="numeric.html" title="9. Numeric and Mathematical Modules"
55
accesskey="N">next</a> |</li>
57
<a href="pprint.html" title="8.18. pprint — Data pretty printer"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.11</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="repr-alternate-repr-implementation">
78
<h1>8.19. <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-mod docutils literal"><span class="pre">repr</span></code></a> — Alternate <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a> implementation<a class="headerlink" href="#repr-alternate-repr-implementation" title="Permalink to this headline">¶</a></h1>
79
<div class="admonition note">
80
<p class="first admonition-title">Note</p>
81
<p class="last">The <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-mod docutils literal"><span class="pre">repr</span></code></a> module has been renamed to <code class="xref py py-mod docutils literal"><span class="pre">reprlib</span></code> in Python 3. The
82
<a class="reference internal" href="../glossary.html#term-2to3"><span class="xref std std-term">2to3</span></a> tool will automatically adapt imports when converting your
83
sources to Python 3.</p>
85
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/repr.py">Lib/repr.py</a></p>
86
<hr class="docutils" />
87
<p>The <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-mod docutils literal"><span class="pre">repr</span></code></a> module provides a means for producing object representations
88
with limits on the size of the resulting strings. This is used in the Python
89
debugger and may be useful in other contexts as well.</p>
90
<p>This module provides a class, an instance, and a function:</p>
93
<em class="property">class </em><code class="descclassname">repr.</code><code class="descname">Repr</code><a class="headerlink" href="#repr.Repr" title="Permalink to this definition">¶</a></dt>
94
<dd><p>Class which provides formatting services useful in implementing functions
95
similar to the built-in <a class="reference internal" href="functions.html#func-repr"><span>repr()</span></a>; size limits for different
96
object types are added to avoid the generation of representations which are
102
<code class="descclassname">repr.</code><code class="descname">aRepr</code><a class="headerlink" href="#repr.aRepr" title="Permalink to this definition">¶</a></dt>
103
<dd><p>This is an instance of <a class="reference internal" href="#repr.Repr" title="repr.Repr"><code class="xref py py-class docutils literal"><span class="pre">Repr</span></code></a> which is used to provide the <a class="reference internal" href="#repr.repr" title="repr.repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a>
104
function described below. Changing the attributes of this object will affect
105
the size limits used by <a class="reference internal" href="#repr.repr" title="repr.repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a> and the Python debugger.</p>
108
<dl class="function">
110
<code class="descclassname">repr.</code><code class="descname">repr</code><span class="sig-paren">(</span><em>obj</em><span class="sig-paren">)</span><a class="headerlink" href="#repr.repr" title="Permalink to this definition">¶</a></dt>
111
<dd><p>This is the <a class="reference internal" href="#repr.Repr.repr" title="repr.Repr.repr"><code class="xref py py-meth docutils literal"><span class="pre">repr()</span></code></a> method of <code class="docutils literal"><span class="pre">aRepr</span></code>. It returns a string
112
similar to that returned by the built-in function of the same name, but with
113
limits on most sizes.</p>
116
<div class="section" id="repr-objects">
117
<span id="id1"></span><h2>8.19.1. Repr Objects<a class="headerlink" href="#repr-objects" title="Permalink to this headline">¶</a></h2>
118
<p><a class="reference internal" href="#repr.Repr" title="repr.Repr"><code class="xref py py-class docutils literal"><span class="pre">Repr</span></code></a> instances provide several attributes which can be used to provide
119
size limits for the representations of different object types, and methods
120
which format specific object types.</p>
121
<dl class="attribute">
122
<dt id="repr.Repr.maxlevel">
123
<code class="descclassname">Repr.</code><code class="descname">maxlevel</code><a class="headerlink" href="#repr.Repr.maxlevel" title="Permalink to this definition">¶</a></dt>
124
<dd><p>Depth limit on the creation of recursive representations. The default is <code class="docutils literal"><span class="pre">6</span></code>.</p>
127
<dl class="attribute">
128
<dt id="repr.Repr.maxdict">
129
<code class="descclassname">Repr.</code><code class="descname">maxdict</code><a class="headerlink" href="#repr.Repr.maxdict" title="Permalink to this definition">¶</a></dt>
130
<dt id="repr.Repr.maxlist">
131
<code class="descclassname">Repr.</code><code class="descname">maxlist</code><a class="headerlink" href="#repr.Repr.maxlist" title="Permalink to this definition">¶</a></dt>
132
<dt id="repr.Repr.maxtuple">
133
<code class="descclassname">Repr.</code><code class="descname">maxtuple</code><a class="headerlink" href="#repr.Repr.maxtuple" title="Permalink to this definition">¶</a></dt>
134
<dt id="repr.Repr.maxset">
135
<code class="descclassname">Repr.</code><code class="descname">maxset</code><a class="headerlink" href="#repr.Repr.maxset" title="Permalink to this definition">¶</a></dt>
136
<dt id="repr.Repr.maxfrozenset">
137
<code class="descclassname">Repr.</code><code class="descname">maxfrozenset</code><a class="headerlink" href="#repr.Repr.maxfrozenset" title="Permalink to this definition">¶</a></dt>
138
<dt id="repr.Repr.maxdeque">
139
<code class="descclassname">Repr.</code><code class="descname">maxdeque</code><a class="headerlink" href="#repr.Repr.maxdeque" title="Permalink to this definition">¶</a></dt>
140
<dt id="repr.Repr.maxarray">
141
<code class="descclassname">Repr.</code><code class="descname">maxarray</code><a class="headerlink" href="#repr.Repr.maxarray" title="Permalink to this definition">¶</a></dt>
142
<dd><p>Limits on the number of entries represented for the named object type. The
143
default is <code class="docutils literal"><span class="pre">4</span></code> for <a class="reference internal" href="#repr.Repr.maxdict" title="repr.Repr.maxdict"><code class="xref py py-attr docutils literal"><span class="pre">maxdict</span></code></a>, <code class="docutils literal"><span class="pre">5</span></code> for <a class="reference internal" href="#repr.Repr.maxarray" title="repr.Repr.maxarray"><code class="xref py py-attr docutils literal"><span class="pre">maxarray</span></code></a>, and <code class="docutils literal"><span class="pre">6</span></code> for
145
<div class="versionadded">
146
<p><span class="versionmodified">New in version 2.4: </span><a class="reference internal" href="#repr.Repr.maxset" title="repr.Repr.maxset"><code class="xref py py-attr docutils literal"><span class="pre">maxset</span></code></a>, <a class="reference internal" href="#repr.Repr.maxfrozenset" title="repr.Repr.maxfrozenset"><code class="xref py py-attr docutils literal"><span class="pre">maxfrozenset</span></code></a>, and <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-attr docutils literal"><span class="pre">set</span></code></a>.</p>
150
<dl class="attribute">
151
<dt id="repr.Repr.maxlong">
152
<code class="descclassname">Repr.</code><code class="descname">maxlong</code><a class="headerlink" href="#repr.Repr.maxlong" title="Permalink to this definition">¶</a></dt>
153
<dd><p>Maximum number of characters in the representation for a long integer. Digits
154
are dropped from the middle. The default is <code class="docutils literal"><span class="pre">40</span></code>.</p>
157
<dl class="attribute">
158
<dt id="repr.Repr.maxstring">
159
<code class="descclassname">Repr.</code><code class="descname">maxstring</code><a class="headerlink" href="#repr.Repr.maxstring" title="Permalink to this definition">¶</a></dt>
160
<dd><p>Limit on the number of characters in the representation of the string. Note
161
that the “normal” representation of the string is used as the character source:
162
if escape sequences are needed in the representation, these may be mangled when
163
the representation is shortened. The default is <code class="docutils literal"><span class="pre">30</span></code>.</p>
166
<dl class="attribute">
167
<dt id="repr.Repr.maxother">
168
<code class="descclassname">Repr.</code><code class="descname">maxother</code><a class="headerlink" href="#repr.Repr.maxother" title="Permalink to this definition">¶</a></dt>
169
<dd><p>This limit is used to control the size of object types for which no specific
170
formatting method is available on the <a class="reference internal" href="#repr.Repr" title="repr.Repr"><code class="xref py py-class docutils literal"><span class="pre">Repr</span></code></a> object. It is applied in a
171
similar manner as <a class="reference internal" href="#repr.Repr.maxstring" title="repr.Repr.maxstring"><code class="xref py py-attr docutils literal"><span class="pre">maxstring</span></code></a>. The default is <code class="docutils literal"><span class="pre">20</span></code>.</p>
175
<dt id="repr.Repr.repr">
176
<code class="descclassname">Repr.</code><code class="descname">repr</code><span class="sig-paren">(</span><em>obj</em><span class="sig-paren">)</span><a class="headerlink" href="#repr.Repr.repr" title="Permalink to this definition">¶</a></dt>
177
<dd><p>The equivalent to the built-in <a class="reference internal" href="functions.html#func-repr"><span>repr()</span></a> that uses the
178
formatting imposed by the instance.</p>
182
<dt id="repr.Repr.repr1">
183
<code class="descclassname">Repr.</code><code class="descname">repr1</code><span class="sig-paren">(</span><em>obj</em>, <em>level</em><span class="sig-paren">)</span><a class="headerlink" href="#repr.Repr.repr1" title="Permalink to this definition">¶</a></dt>
184
<dd><p>Recursive implementation used by <a class="reference internal" href="#repr.Repr.repr" title="repr.Repr.repr"><code class="xref py py-meth docutils literal"><span class="pre">repr()</span></code></a>. This uses the type of <em>obj</em> to
185
determine which formatting method to call, passing it <em>obj</em> and <em>level</em>. The
186
type-specific methods should call <a class="reference internal" href="#repr.Repr.repr1" title="repr.Repr.repr1"><code class="xref py py-meth docutils literal"><span class="pre">repr1()</span></code></a> to perform recursive formatting,
187
with <code class="docutils literal"><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>
192
<code class="descclassname">Repr.</code><code class="descname">repr_TYPE</code><span class="sig-paren">(</span><em>obj</em>, <em>level</em><span class="sig-paren">)</span></dt>
193
<dd><p>Formatting methods for specific types are implemented as methods with a name
194
based on the type name. In the method name, <strong>TYPE</strong> is replaced by
195
<code class="docutils literal"><span class="pre">string.join(string.split(type(obj).__name__,</span> <span class="pre">'_'))</span></code>. Dispatch to these
196
methods is handled by <a class="reference internal" href="#repr.Repr.repr1" title="repr.Repr.repr1"><code class="xref py py-meth docutils literal"><span class="pre">repr1()</span></code></a>. Type-specific methods which need to
197
recursively format a value should call <code class="docutils literal"><span class="pre">self.repr1(subobj,</span> <span class="pre">level</span> <span class="pre">-</span> <span class="pre">1)</span></code>.</p>
201
<div class="section" id="subclassing-repr-objects">
202
<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>
203
<p>The use of dynamic dispatching by <a class="reference internal" href="#repr.Repr.repr1" title="repr.Repr.repr1"><code class="xref py py-meth docutils literal"><span class="pre">Repr.repr1()</span></code></a> allows subclasses of
204
<a class="reference internal" href="#repr.Repr" title="repr.Repr"><code class="xref py py-class docutils literal"><span class="pre">Repr</span></code></a> to add support for additional built-in object types or to modify
205
the handling of types already supported. This example shows how special support
206
for file objects could be added:</p>
207
<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>
208
<span class="kn">import</span> <span class="nn">sys</span>
210
<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>
211
<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>
212
<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>
213
<span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">name</span>
214
<span class="k">else</span><span class="p">:</span>
215
<span class="k">return</span> <span class="nb">repr</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
217
<span class="n">aRepr</span> <span class="o">=</span> <span class="n">MyRepr</span><span class="p">()</span>
218
<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="c1"># prints '<stdin>'</span>
228
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
229
<div class="sphinxsidebarwrapper">
230
<h3><a href="../contents.html">Table Of Contents</a></h3>
232
<li><a class="reference internal" href="#">8.19. <code class="docutils literal"><span class="pre">repr</span></code> — Alternate <code class="docutils literal"><span class="pre">repr()</span></code> implementation</a><ul>
233
<li><a class="reference internal" href="#repr-objects">8.19.1. Repr Objects</a></li>
234
<li><a class="reference internal" href="#subclassing-repr-objects">8.19.2. Subclassing Repr Objects</a></li>
239
<h4>Previous topic</h4>
240
<p class="topless"><a href="pprint.html"
241
title="previous chapter">8.18. <code class="docutils literal"><span class="pre">pprint</span></code> — Data pretty printer</a></p>
243
<p class="topless"><a href="numeric.html"
244
title="next chapter">9. Numeric and Mathematical Modules</a></p>
246
<ul class="this-page-menu">
247
<li><a href="../bugs.html">Report a Bug</a></li>
248
<li><a href="../_sources/library/repr.txt"
249
rel="nofollow">Show Source</a></li>
252
<div id="searchbox" style="display: none" role="search">
253
<h3>Quick search</h3>
254
<form class="search" action="../search.html" method="get">
255
<input type="text" name="q" />
256
<input type="submit" value="Go" />
257
<input type="hidden" name="check_keywords" value="yes" />
258
<input type="hidden" name="area" value="default" />
260
<p class="searchtip" style="font-size: 90%">
261
Enter search terms or a module, class or function name.
264
<script type="text/javascript">$('#searchbox').show(0);</script>
267
<div class="clearer"></div>
269
<div class="related" role="navigation" aria-label="related navigation">
272
<li class="right" style="margin-right: 10px">
273
<a href="../genindex.html" title="General Index"
276
<a href="../py-modindex.html" title="Python Module Index"
279
<a href="numeric.html" title="9. Numeric and Mathematical Modules"
282
<a href="pprint.html" title="8.18. pprint — Data pretty printer"
284
<li><img src="../_static/py.png" alt=""
285
style="vertical-align: middle; margin-top: -1px"/></li>
286
<li><a href="https://www.python.org/">Python</a> »</li>
288
<span class="version_switcher_placeholder">2.7.11</span>
289
<a href="../index.html">Documentation</a> »
292
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
293
<li class="nav-item nav-item-2"><a href="datatypes.html" >8. Data Types</a> »</li>
297
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
299
The Python Software Foundation is a non-profit corporation.
300
<a href="https://www.python.org/psf/donations/">Please donate.</a>
302
Last updated on Jan 23, 2016.
303
<a href="../bugs.html">Found a bug</a>?
305
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'