| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="module-operator.html" /> |
| 13 | <link rel="prev" href="module-UserList.html" /> |
| 14 | <link rel="parent" href="python.html" /> |
| 15 | <link rel="next" href="module-operator.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.9 UserString -- Class wrapper for string objects</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.8 UserList " |
| 25 | href="module-UserList.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 28 | href="python.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.10 operator " |
| 31 | href="module-operator.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-UserList.html">3.8 UserList </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="module-operator.html">3.10 operator </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION005900000000000000000"> |
| 56 | 3.9 <tt class="module">UserString</tt> -- |
| 57 | Class wrapper for string objects</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-UserString"></A> |
| 62 | |
| 63 | <P> |
| 64 | <span class="note"><b class="label">Note:</b> |
| 65 | This <tt class="class">UserString</tt> class from this module is available for |
| 66 | backward compatibility only. If you are writing code that does not |
| 67 | need to work with versions of Python earlier than Python 2.2, please |
| 68 | consider subclassing directly from the built-in <tt class="class">str</tt> type |
| 69 | instead of using <tt class="class">UserString</tt> (there is no built-in equivalent |
| 70 | to <tt class="class">MutableString</tt>).</span> |
| 71 | |
| 72 | <P> |
| 73 | This module defines a class that acts as a wrapper around string |
| 74 | objects. It is a useful base class for your own string-like classes, |
| 75 | which can inherit from them and override existing methods or add new |
| 76 | ones. In this way one can add new behaviors to strings. |
| 77 | |
| 78 | <P> |
| 79 | It should be noted that these classes are highly inefficient compared |
| 80 | to real string or Unicode objects; this is especially the case for |
| 81 | <tt class="class">MutableString</tt>. |
| 82 | |
| 83 | <P> |
| 84 | The <tt class="module">UserString</tt> module defines the following classes: |
| 85 | |
| 86 | <P> |
| 87 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 88 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-486' xml:id='l2h-486' class="class">UserString</tt></b>(</nobr></td> |
| 89 | <td><var></var><big>[</big><var>sequence</var><big>]</big><var></var>)</td></tr></table></dt> |
| 90 | <dd> |
| 91 | Class that simulates a string or a Unicode string |
| 92 | object. The instance's content is kept in a regular string or Unicode |
| 93 | string object, which is accessible via the <tt class="member">data</tt> attribute of |
| 94 | <tt class="class">UserString</tt> instances. The instance's contents are initially |
| 95 | set to a copy of <var>sequence</var>. <var>sequence</var> can be either a |
| 96 | regular Python string or Unicode string, an instance of |
| 97 | <tt class="class">UserString</tt> (or a subclass) or an arbitrary sequence which can |
| 98 | be converted into a string using the built-in <tt class="function">str()</tt> function. |
| 99 | </dl> |
| 100 | |
| 101 | <P> |
| 102 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 103 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-487' xml:id='l2h-487' class="class">MutableString</tt></b>(</nobr></td> |
| 104 | <td><var></var><big>[</big><var>sequence</var><big>]</big><var></var>)</td></tr></table></dt> |
| 105 | <dd> |
| 106 | This class is derived from the <tt class="class">UserString</tt> above and redefines |
| 107 | strings to be <em>mutable</em>. Mutable strings can't be used as |
| 108 | dictionary keys, because dictionaries require <em>immutable</em> objects as |
| 109 | keys. The main intention of this class is to serve as an educational |
| 110 | example for inheritance and necessity to remove (override) the |
| 111 | <tt class="method">__hash__()</tt> method in order to trap attempts to use a |
| 112 | mutable object as dictionary key, which would be otherwise very |
| 113 | error prone and hard to track down. |
| 114 | </dl> |
| 115 | |
| 116 | <P> |
| 117 | In addition to supporting the methods and operations of string and |
| 118 | Unicode objects (see section <A href="string-methods.html#string-methods">2.3.6</A>, ``String |
| 119 | Methods''), <tt class="class">UserString</tt> instances provide the following |
| 120 | attribute: |
| 121 | |
| 122 | <P> |
| 123 | <dl><dt><b><tt id='l2h-488' xml:id='l2h-488' class="member">data</tt></b></dt> |
| 124 | <dd> |
| 125 | A real Python string or Unicode object used to store the content of the |
| 126 | <tt class="class">UserString</tt> class. |
| 127 | </dl> |
| 128 | |
| 129 | <DIV CLASS="navigation"> |
| 130 | <div class='online-navigation'> |
| 131 | <p></p><hr /> |
| 132 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 133 | <tr> |
| 134 | <td class='online-navigation'><a rel="prev" title="3.8 UserList " |
| 135 | href="module-UserList.html"><img src='../icons/previous.png' |
| 136 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 137 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 138 | href="python.html"><img src='../icons/up.png' |
| 139 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 140 | <td class='online-navigation'><a rel="next" title="3.10 operator " |
| 141 | href="module-operator.html"><img src='../icons/next.png' |
| 142 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 143 | <td align="center" width="100%">Python Library Reference</td> |
| 144 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 145 | href="contents.html"><img src='../icons/contents.png' |
| 146 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 147 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 148 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 149 | <td class='online-navigation'><a rel="index" title="Index" |
| 150 | href="genindex.html"><img src='../icons/index.png' |
| 151 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 152 | </tr></table> |
| 153 | <div class='online-navigation'> |
| 154 | <b class="navlabel">Previous:</b> |
| 155 | <a class="sectref" rel="prev" href="module-UserList.html">3.8 UserList </A> |
| 156 | <b class="navlabel">Up:</b> |
| 157 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 158 | <b class="navlabel">Next:</b> |
| 159 | <a class="sectref" rel="next" href="module-operator.html">3.10 operator </A> |
| 160 | </div> |
| 161 | </div> |
| 162 | <hr /> |
| 163 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 164 | </DIV> |
| 165 | <!--End of Navigation Panel--> |
| 166 | <ADDRESS> |
| 167 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 168 | </ADDRESS> |
| 169 | </BODY> |
| 170 | </HTML> |