~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/library/userdict.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.12. UserDict — Class wrapper for dictionary objects &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="8.15. types — Names for built-in types" href="types.html" />
35
 
    <link rel="prev" title="8.11. weakref — Weak references" href="weakref.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="types.html" title="8.15. types — Names for built-in types"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="weakref.html" title="8.11. weakref — Weak references"
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="module-UserDict">
78
 
<span id="userdict-class-wrapper-for-dictionary-objects"></span><h1>8.12. <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-mod docutils literal"><span class="pre">UserDict</span></code></a> &#8212; Class wrapper for dictionary objects<a class="headerlink" href="#module-UserDict" title="Permalink to this headline">¶</a></h1>
79
 
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/UserDict.py">Lib/UserDict.py</a></p>
80
 
<hr class="docutils" />
81
 
<p>The module defines a mixin,  <a class="reference internal" href="#UserDict.DictMixin" title="UserDict.DictMixin"><code class="xref py py-class docutils literal"><span class="pre">DictMixin</span></code></a>, defining all dictionary methods
82
 
for classes that already have a minimum mapping interface.  This greatly
83
 
simplifies writing classes that need to be substitutable for dictionaries (such
84
 
as the shelve module).</p>
85
 
<p>This module also defines a class, <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a>, that acts as a wrapper
86
 
around dictionary objects.  The need for this class has been largely supplanted
87
 
by the ability to subclass directly from <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal"><span class="pre">dict</span></code></a> (a feature that became
88
 
available starting with Python version 2.2).  Prior to the introduction of
89
 
<a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal"><span class="pre">dict</span></code></a>, the <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a> class was used to create dictionary-like
90
 
sub-classes that obtained new behaviors by overriding existing methods or adding
91
 
new ones.</p>
92
 
<p>The <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-mod docutils literal"><span class="pre">UserDict</span></code></a> module defines the <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a> class and
93
 
<a class="reference internal" href="#UserDict.DictMixin" title="UserDict.DictMixin"><code class="xref py py-class docutils literal"><span class="pre">DictMixin</span></code></a>:</p>
94
 
<dl class="class">
95
 
<dt id="UserDict.UserDict">
96
 
<em class="property">class </em><code class="descclassname">UserDict.</code><code class="descname">UserDict</code><span class="sig-paren">(</span><span class="optional">[</span><em>initialdata</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#UserDict.UserDict" title="Permalink to this definition">¶</a></dt>
97
 
<dd><p>Class that simulates a dictionary.  The instance&#8217;s contents are kept in a
98
 
regular dictionary, which is accessible via the <code class="xref py py-attr docutils literal"><span class="pre">data</span></code> attribute of
99
 
<a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a> instances.  If <em>initialdata</em> is provided, <code class="xref py py-attr docutils literal"><span class="pre">data</span></code> is
100
 
initialized with its contents; note that a reference to <em>initialdata</em> will not
101
 
be kept, allowing it be used for other purposes.</p>
102
 
<div class="admonition note">
103
 
<p class="first admonition-title">Note</p>
104
 
<p class="last">For backward compatibility, instances of <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a> are not iterable.</p>
105
 
</div>
106
 
</dd></dl>
107
 
 
108
 
<dl class="class">
109
 
<dt id="UserDict.IterableUserDict">
110
 
<em class="property">class </em><code class="descclassname">UserDict.</code><code class="descname">IterableUserDict</code><span class="sig-paren">(</span><span class="optional">[</span><em>initialdata</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#UserDict.IterableUserDict" title="Permalink to this definition">¶</a></dt>
111
 
<dd><p>Subclass of <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a> that supports direct iteration (e.g.  <code class="docutils literal"><span class="pre">for</span> <span class="pre">key</span> <span class="pre">in</span>
112
 
<span class="pre">myDict</span></code>).</p>
113
 
</dd></dl>
114
 
 
115
 
<p>In addition to supporting the methods and operations of mappings (see section
116
 
<a class="reference internal" href="stdtypes.html#typesmapping"><span>Mapping Types &#8212; dict</span></a>), <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a> and <a class="reference internal" href="#UserDict.IterableUserDict" title="UserDict.IterableUserDict"><code class="xref py py-class docutils literal"><span class="pre">IterableUserDict</span></code></a> instances
117
 
provide the following attribute:</p>
118
 
<dl class="attribute">
119
 
<dt id="UserDict.IterableUserDict.data">
120
 
<code class="descclassname">IterableUserDict.</code><code class="descname">data</code><a class="headerlink" href="#UserDict.IterableUserDict.data" title="Permalink to this definition">¶</a></dt>
121
 
<dd><p>A real dictionary used to store the contents of the <a class="reference internal" href="#module-UserDict" title="UserDict: Class wrapper for dictionary objects."><code class="xref py py-class docutils literal"><span class="pre">UserDict</span></code></a> class.</p>
122
 
</dd></dl>
123
 
 
124
 
<dl class="class">
125
 
<dt id="UserDict.DictMixin">
126
 
<em class="property">class </em><code class="descclassname">UserDict.</code><code class="descname">DictMixin</code><a class="headerlink" href="#UserDict.DictMixin" title="Permalink to this definition">¶</a></dt>
127
 
<dd><p>Mixin defining all dictionary methods for classes that already have a minimum
128
 
dictionary interface including <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__setitem__" title="object.__setitem__"><code class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></code></a>,
129
 
<a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></code></a>, and <code class="xref py py-meth docutils literal"><span class="pre">keys()</span></code>.</p>
130
 
<p>This mixin should be used as a superclass.  Adding each of the above methods
131
 
adds progressively more functionality.  For instance, defining all but
132
 
<a class="reference internal" href="../reference/datamodel.html#object.__delitem__" title="object.__delitem__"><code class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></code></a> will preclude only <code class="xref py py-meth docutils literal"><span class="pre">pop()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">popitem()</span></code> from the
133
 
full interface.</p>
134
 
<p>In addition to the four base methods, progressively more efficiency comes with
135
 
defining <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><code class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></code></a>, and <code class="xref py py-meth docutils literal"><span class="pre">iteritems()</span></code>.</p>
136
 
<p>Since the mixin has no knowledge of the subclass constructor, it does not define
137
 
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><code class="xref py py-meth docutils literal"><span class="pre">__init__()</span></code></a> or <a class="reference internal" href="copy.html#module-copy" title="copy: Shallow and deep copy operations."><code class="xref py py-meth docutils literal"><span class="pre">copy()</span></code></a>.</p>
138
 
<p>Starting with Python version 2.6, it is recommended to use
139
 
<a class="reference internal" href="collections.html#collections.MutableMapping" title="collections.MutableMapping"><code class="xref py py-class docutils literal"><span class="pre">collections.MutableMapping</span></code></a> instead of <a class="reference internal" href="#UserDict.DictMixin" title="UserDict.DictMixin"><code class="xref py py-class docutils literal"><span class="pre">DictMixin</span></code></a>.</p>
140
 
</dd></dl>
141
 
 
142
 
</div>
143
 
<div class="section" id="module-UserList">
144
 
<span id="userlist-class-wrapper-for-list-objects"></span><h1>8.13. <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-mod docutils literal"><span class="pre">UserList</span></code></a> &#8212; Class wrapper for list objects<a class="headerlink" href="#module-UserList" title="Permalink to this headline">¶</a></h1>
145
 
<div class="admonition note">
146
 
<p class="first admonition-title">Note</p>
147
 
<p>When Python 2.2 was released, many of the use cases for this class were
148
 
subsumed by the ability to subclass <a class="reference internal" href="functions.html#list" title="list"><code class="xref py py-class docutils literal"><span class="pre">list</span></code></a> directly.  However, a
149
 
handful of use cases remain.</p>
150
 
<p>This module provides a list-interface around an underlying data store.  By
151
 
default, that data store is a <a class="reference internal" href="functions.html#list" title="list"><code class="xref py py-class docutils literal"><span class="pre">list</span></code></a>; however, it can be used to wrap
152
 
a list-like interface around other objects (such as persistent storage).</p>
153
 
<p class="last">In addition, this class can be mixed-in with built-in classes using multiple
154
 
inheritance.  This can sometimes be useful.  For example, you can inherit
155
 
from <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a> and <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> at the same time.  That would not be
156
 
possible with both a real <a class="reference internal" href="functions.html#list" title="list"><code class="xref py py-class docutils literal"><span class="pre">list</span></code></a> and a real <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a>.</p>
157
 
</div>
158
 
<p>This module defines a class that acts as a wrapper around list objects.  It is a
159
 
useful base class for your own list-like classes, which can inherit from them
160
 
and override existing methods or add new ones.  In this way one can add new
161
 
behaviors to lists.</p>
162
 
<p>The <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-mod docutils literal"><span class="pre">UserList</span></code></a> module defines the <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a> class:</p>
163
 
<dl class="class">
164
 
<dt id="UserList.UserList">
165
 
<em class="property">class </em><code class="descclassname">UserList.</code><code class="descname">UserList</code><span class="sig-paren">(</span><span class="optional">[</span><em>list</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#UserList.UserList" title="Permalink to this definition">¶</a></dt>
166
 
<dd><p>Class that simulates a list.  The instance&#8217;s contents are kept in a regular
167
 
list, which is accessible via the <a class="reference internal" href="#UserList.UserList.data" title="UserList.UserList.data"><code class="xref py py-attr docutils literal"><span class="pre">data</span></code></a> attribute of <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a>
168
 
instances.  The instance&#8217;s contents are initially set to a copy of <em>list</em>,
169
 
defaulting to the empty list <code class="docutils literal"><span class="pre">[]</span></code>.  <em>list</em> can be any iterable, e.g. a
170
 
real Python list or a <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a> object.</p>
171
 
<div class="admonition note">
172
 
<p class="first admonition-title">Note</p>
173
 
<p class="last">The <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a> class has been moved to the <a class="reference internal" href="collections.html#module-collections" title="collections: High-performance datatypes"><code class="xref py py-mod docutils literal"><span class="pre">collections</span></code></a>
174
 
module in Python 3. The <a class="reference internal" href="../glossary.html#term-2to3"><span class="xref std std-term">2to3</span></a> tool will automatically adapt
175
 
imports when converting your sources to Python 3.</p>
176
 
</div>
177
 
</dd></dl>
178
 
 
179
 
<p>In addition to supporting the methods and operations of mutable sequences (see
180
 
section <a class="reference internal" href="stdtypes.html#typesseq"><span>Sequence Types &#8212; str, unicode, list, tuple, bytearray, buffer, xrange</span></a>), <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a> instances provide the following
181
 
attribute:</p>
182
 
<dl class="attribute">
183
 
<dt id="UserList.UserList.data">
184
 
<code class="descclassname">UserList.</code><code class="descname">data</code><a class="headerlink" href="#UserList.UserList.data" title="Permalink to this definition">¶</a></dt>
185
 
<dd><p>A real Python list object used to store the contents of the <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a>
186
 
class.</p>
187
 
</dd></dl>
188
 
 
189
 
<p><strong>Subclassing requirements:</strong> Subclasses of <a class="reference internal" href="#module-UserList" title="UserList: Class wrapper for list objects."><code class="xref py py-class docutils literal"><span class="pre">UserList</span></code></a> are expected to
190
 
offer a constructor which can be called with either no arguments or one
191
 
argument.  List operations which return a new sequence attempt to create an
192
 
instance of the actual implementation class.  To do so, it assumes that the
193
 
constructor can be called with a single parameter, which is a sequence object
194
 
used as a data source.</p>
195
 
<p>If a derived class does not wish to comply with this requirement, all of the
196
 
special methods supported by this class will need to be overridden; please
197
 
consult the sources for information about the methods which need to be provided
198
 
in that case.</p>
199
 
<div class="versionchanged">
200
 
<p><span class="versionmodified">Changed in version 2.0: </span>Python versions 1.5.2 and 1.6 also required that the constructor be callable
201
 
with no parameters, and offer a mutable <code class="xref py py-attr docutils literal"><span class="pre">data</span></code> attribute.  Earlier
202
 
versions of Python did not attempt to create instances of the derived class.</p>
203
 
</div>
204
 
</div>
205
 
<div class="section" id="module-UserString">
206
 
<span id="userstring-class-wrapper-for-string-objects"></span><h1>8.14. <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-mod docutils literal"><span class="pre">UserString</span></code></a> &#8212; Class wrapper for string objects<a class="headerlink" href="#module-UserString" title="Permalink to this headline">¶</a></h1>
207
 
<div class="admonition note">
208
 
<p class="first admonition-title">Note</p>
209
 
<p class="last">This <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> class from this module is available for backward
210
 
compatibility only.  If you are writing code that does not need to work with
211
 
versions of Python earlier than Python 2.2, please consider subclassing directly
212
 
from the built-in <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> type instead of using <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> (there
213
 
is no built-in equivalent to <a class="reference internal" href="#UserString.MutableString" title="UserString.MutableString"><code class="xref py py-class docutils literal"><span class="pre">MutableString</span></code></a>).</p>
214
 
</div>
215
 
<p>This module defines a class that acts as a wrapper around string objects.  It is
216
 
a useful base class for your own string-like classes, which can inherit from
217
 
them and override existing methods or add new ones.  In this way one can add new
218
 
behaviors to strings.</p>
219
 
<p>It should be noted that these classes are highly inefficient compared to real
220
 
string or Unicode objects; this is especially the case for
221
 
<a class="reference internal" href="#UserString.MutableString" title="UserString.MutableString"><code class="xref py py-class docutils literal"><span class="pre">MutableString</span></code></a>.</p>
222
 
<p>The <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-mod docutils literal"><span class="pre">UserString</span></code></a> module defines the following classes:</p>
223
 
<dl class="class">
224
 
<dt id="UserString.UserString">
225
 
<em class="property">class </em><code class="descclassname">UserString.</code><code class="descname">UserString</code><span class="sig-paren">(</span><span class="optional">[</span><em>sequence</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#UserString.UserString" title="Permalink to this definition">¶</a></dt>
226
 
<dd><p>Class that simulates a string or a Unicode string object.  The instance&#8217;s
227
 
content is kept in a regular string or Unicode string object, which is
228
 
accessible via the <code class="xref py py-attr docutils literal"><span class="pre">data</span></code> attribute of <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> instances.  The
229
 
instance&#8217;s contents are initially set to a copy of <em>sequence</em>.  <em>sequence</em> can
230
 
be either a regular Python string or Unicode string, an instance of
231
 
<a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> (or a subclass) or an arbitrary sequence which can be
232
 
converted into a string using the built-in <a class="reference internal" href="functions.html#str" title="str"><code class="xref py py-func docutils literal"><span class="pre">str()</span></code></a> function.</p>
233
 
<div class="admonition note">
234
 
<p class="first admonition-title">Note</p>
235
 
<p class="last">The <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> class has been moved to the <a class="reference internal" href="collections.html#module-collections" title="collections: High-performance datatypes"><code class="xref py py-mod docutils literal"><span class="pre">collections</span></code></a>
236
 
module in Python 3. The <a class="reference internal" href="../glossary.html#term-2to3"><span class="xref std std-term">2to3</span></a> tool will automatically adapt
237
 
imports when converting your sources to Python 3.</p>
238
 
</div>
239
 
</dd></dl>
240
 
 
241
 
<dl class="class">
242
 
<dt id="UserString.MutableString">
243
 
<em class="property">class </em><code class="descclassname">UserString.</code><code class="descname">MutableString</code><span class="sig-paren">(</span><span class="optional">[</span><em>sequence</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#UserString.MutableString" title="Permalink to this definition">¶</a></dt>
244
 
<dd><p>This class is derived from the <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> above and redefines strings
245
 
to be <em>mutable</em>.  Mutable strings can&#8217;t be used as dictionary keys, because
246
 
dictionaries require <em>immutable</em> objects as keys.  The main intention of this
247
 
class is to serve as an educational example for inheritance and necessity to
248
 
remove (override) the <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a> method in order to trap attempts to use a
249
 
mutable object as dictionary key, which would be otherwise very error prone and
250
 
hard to track down.</p>
251
 
<div class="deprecated">
252
 
<p><span class="versionmodified">Deprecated since version 2.6: </span>The <a class="reference internal" href="#UserString.MutableString" title="UserString.MutableString"><code class="xref py py-class docutils literal"><span class="pre">MutableString</span></code></a> class has been removed in Python 3.</p>
253
 
</div>
254
 
</dd></dl>
255
 
 
256
 
<p>In addition to supporting the methods and operations of string and Unicode
257
 
objects (see section <a class="reference internal" href="stdtypes.html#string-methods"><span>String Methods</span></a>), <a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> instances
258
 
provide the following attribute:</p>
259
 
<dl class="attribute">
260
 
<dt id="UserString.MutableString.data">
261
 
<code class="descclassname">MutableString.</code><code class="descname">data</code><a class="headerlink" href="#UserString.MutableString.data" title="Permalink to this definition">¶</a></dt>
262
 
<dd><p>A real Python string or Unicode object used to store the content of the
263
 
<a class="reference internal" href="#module-UserString" title="UserString: Class wrapper for string objects."><code class="xref py py-class docutils literal"><span class="pre">UserString</span></code></a> class.</p>
264
 
</dd></dl>
265
 
 
266
 
</div>
267
 
 
268
 
 
269
 
          </div>
270
 
        </div>
271
 
      </div>
272
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
273
 
        <div class="sphinxsidebarwrapper">
274
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
275
 
  <ul>
276
 
<li><a class="reference internal" href="#">8.12. <code class="docutils literal"><span class="pre">UserDict</span></code> &#8212; Class wrapper for dictionary objects</a></li>
277
 
<li><a class="reference internal" href="#module-UserList">8.13. <code class="docutils literal"><span class="pre">UserList</span></code> &#8212; Class wrapper for list objects</a></li>
278
 
<li><a class="reference internal" href="#module-UserString">8.14. <code class="docutils literal"><span class="pre">UserString</span></code> &#8212; Class wrapper for string objects</a></li>
279
 
</ul>
280
 
 
281
 
  <h4>Previous topic</h4>
282
 
  <p class="topless"><a href="weakref.html"
283
 
                        title="previous chapter">8.11. <code class="docutils literal"><span class="pre">weakref</span></code> &#8212; Weak references</a></p>
284
 
  <h4>Next topic</h4>
285
 
  <p class="topless"><a href="types.html"
286
 
                        title="next chapter">8.15. <code class="docutils literal"><span class="pre">types</span></code> &#8212; Names for built-in types</a></p>
287
 
<h3>This Page</h3>
288
 
<ul class="this-page-menu">
289
 
  <li><a href="../bugs.html">Report a Bug</a></li>
290
 
  <li><a href="../_sources/library/userdict.txt"
291
 
         rel="nofollow">Show Source</a></li>
292
 
</ul>
293
 
 
294
 
<div id="searchbox" style="display: none" role="search">
295
 
  <h3>Quick search</h3>
296
 
    <form class="search" action="../search.html" method="get">
297
 
      <input type="text" name="q" />
298
 
      <input type="submit" value="Go" />
299
 
      <input type="hidden" name="check_keywords" value="yes" />
300
 
      <input type="hidden" name="area" value="default" />
301
 
    </form>
302
 
    <p class="searchtip" style="font-size: 90%">
303
 
    Enter search terms or a module, class or function name.
304
 
    </p>
305
 
</div>
306
 
<script type="text/javascript">$('#searchbox').show(0);</script>
307
 
        </div>
308
 
      </div>
309
 
      <div class="clearer"></div>
310
 
    </div>  
311
 
    <div class="related" role="navigation" aria-label="related navigation">
312
 
      <h3>Navigation</h3>
313
 
      <ul>
314
 
        <li class="right" style="margin-right: 10px">
315
 
          <a href="../genindex.html" title="General Index"
316
 
             >index</a></li>
317
 
        <li class="right" >
318
 
          <a href="../py-modindex.html" title="Python Module Index"
319
 
             >modules</a> |</li>
320
 
        <li class="right" >
321
 
          <a href="types.html" title="8.15. types — Names for built-in types"
322
 
             >next</a> |</li>
323
 
        <li class="right" >
324
 
          <a href="weakref.html" title="8.11. weakref — Weak references"
325
 
             >previous</a> |</li>
326
 
        <li><img src="../_static/py.png" alt=""
327
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
328
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
329
 
        <li>
330
 
          <span class="version_switcher_placeholder">2.7.11</span>
331
 
          <a href="../index.html">Documentation</a> &raquo;
332
 
        </li>
333
 
 
334
 
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
335
 
          <li class="nav-item nav-item-2"><a href="datatypes.html" >8. Data Types</a> &raquo;</li> 
336
 
      </ul>
337
 
    </div>  
338
 
    <div class="footer">
339
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
340
 
    <br />
341
 
    The Python Software Foundation is a non-profit corporation.
342
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
343
 
    <br />
344
 
    Last updated on Jan 23, 2016.
345
 
    <a href="../bugs.html">Found a bug</a>?
346
 
    <br />
347
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
348
 
    </div>
349
 
 
350
 
  </body>
351
 
</html>
 
 
b'\\ No newline at end of file'