~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/library/repr.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>8.19. repr — Alternate repr() implementation &mdash; Python 2.7.11 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '2.7.11',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
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>
39
 
 
40
 
    
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="numeric.html" title="9. Numeric and Mathematical Modules"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
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> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">2.7.11</span>
64
 
          <a href="../index.html">Documentation</a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. Data Types</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body" role="main">
76
 
            
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> &#8212; 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>
84
 
</div>
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>
91
 
<dl class="class">
92
 
<dt id="repr.Repr">
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
97
 
excessively long.</p>
98
 
</dd></dl>
99
 
 
100
 
<dl class="data">
101
 
<dt id="repr.aRepr">
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>
106
 
</dd></dl>
107
 
 
108
 
<dl class="function">
109
 
<dt id="repr.repr">
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>
114
 
</dd></dl>
115
 
 
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>
125
 
</dd></dl>
126
 
 
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
144
 
the others.</p>
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>
147
 
</div>
148
 
</dd></dl>
149
 
 
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>
155
 
</dd></dl>
156
 
 
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 &#8220;normal&#8221; 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>
164
 
</dd></dl>
165
 
 
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>
172
 
</dd></dl>
173
 
 
174
 
<dl class="method">
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>
179
 
</dd></dl>
180
 
 
181
 
<dl class="method">
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>
188
 
</dd></dl>
189
 
 
190
 
<dl class="method">
191
 
<dt>
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>
198
 
</dd></dl>
199
 
 
200
 
</div>
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>
209
 
 
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">&#39;&lt;stdin&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;stdout&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;stderr&gt;&#39;</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>
216
 
 
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 &#39;&lt;stdin&gt;&#39;</span>
219
 
</pre></div>
220
 
</div>
221
 
</div>
222
 
</div>
223
 
 
224
 
 
225
 
          </div>
226
 
        </div>
227
 
      </div>
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>
231
 
  <ul>
232
 
<li><a class="reference internal" href="#">8.19. <code class="docutils literal"><span class="pre">repr</span></code> &#8212; 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>
235
 
</ul>
236
 
</li>
237
 
</ul>
238
 
 
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> &#8212; Data pretty printer</a></p>
242
 
  <h4>Next topic</h4>
243
 
  <p class="topless"><a href="numeric.html"
244
 
                        title="next chapter">9. Numeric and Mathematical Modules</a></p>
245
 
<h3>This Page</h3>
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>
250
 
</ul>
251
 
 
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" />
259
 
    </form>
260
 
    <p class="searchtip" style="font-size: 90%">
261
 
    Enter search terms or a module, class or function name.
262
 
    </p>
263
 
</div>
264
 
<script type="text/javascript">$('#searchbox').show(0);</script>
265
 
        </div>
266
 
      </div>
267
 
      <div class="clearer"></div>
268
 
    </div>  
269
 
    <div class="related" role="navigation" aria-label="related navigation">
270
 
      <h3>Navigation</h3>
271
 
      <ul>
272
 
        <li class="right" style="margin-right: 10px">
273
 
          <a href="../genindex.html" title="General Index"
274
 
             >index</a></li>
275
 
        <li class="right" >
276
 
          <a href="../py-modindex.html" title="Python Module Index"
277
 
             >modules</a> |</li>
278
 
        <li class="right" >
279
 
          <a href="numeric.html" title="9. Numeric and Mathematical Modules"
280
 
             >next</a> |</li>
281
 
        <li class="right" >
282
 
          <a href="pprint.html" title="8.18. pprint — Data pretty printer"
283
 
             >previous</a> |</li>
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> &raquo;</li>
287
 
        <li>
288
 
          <span class="version_switcher_placeholder">2.7.11</span>
289
 
          <a href="../index.html">Documentation</a> &raquo;
290
 
        </li>
291
 
 
292
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
293
 
          <li class="nav-item nav-item-2"><a href="datatypes.html" >8. Data Types</a> &raquo;</li> 
294
 
      </ul>
295
 
    </div>  
296
 
    <div class="footer">
297
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
298
 
    <br />
299
 
    The Python Software Foundation is a non-profit corporation.
300
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
301
 
    <br />
302
 
    Last updated on Jan 23, 2016.
303
 
    <a href="../bugs.html">Found a bug</a>?
304
 
    <br />
305
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
306
 
    </div>
307
 
 
308
 
  </body>
309
 
</html>
 
 
b'\\ No newline at end of file'