| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.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="ref.html" title='Python Reference Manual' /> |
| 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="coercion-rules.html" /> |
| 13 | <link rel="prev" href="sequence-methods.html" /> |
| 14 | <link rel="parent" href="specialnames.html" /> |
| 15 | <link rel="next" href="coercion-rules.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.3.7 Emulating numeric types</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.3.6 Additional methods for" |
| 25 | href="sequence-methods.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.3 Special method names" |
| 28 | href="specialnames.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.3.8 Coercion rules" |
| 31 | href="coercion-rules.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 Reference Manual</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'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></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="sequence-methods.html">3.3.6 Additional methods for</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="coercion-rules.html">3.3.8 Coercion rules</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION005370000000000000000"></A><A NAME="numeric-types"></A> |
| 56 | <BR> |
| 57 | 3.3.7 Emulating numeric types |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | The following methods can be defined to emulate numeric objects. |
| 62 | Methods corresponding to operations that are not supported by the |
| 63 | particular kind of number implemented (e.g., bitwise operations for |
| 64 | non-integral numbers) should be left undefined. |
| 65 | |
| 66 | <P> |
| 67 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 68 | <td><nobr><b><tt id='l2h-240' xml:id='l2h-240' class="method">__add__</tt></b>(</nobr></td> |
| 69 | <td><var>self, other</var>)</td></tr></table></dt> |
| 70 | <dd> |
| 71 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 72 | <td><nobr><b><tt id='l2h-241' xml:id='l2h-241' class="method">__sub__</tt></b>(</nobr></td> |
| 73 | <td><var>self, other</var>)</td></tr></table></dt> |
| 74 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 75 | <td><nobr><b><tt id='l2h-242' xml:id='l2h-242' class="method">__mul__</tt></b>(</nobr></td> |
| 76 | <td><var>self, other</var>)</td></tr></table></dt> |
| 77 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 78 | <td><nobr><b><tt id='l2h-243' xml:id='l2h-243' class="method">__floordiv__</tt></b>(</nobr></td> |
| 79 | <td><var>self, other</var>)</td></tr></table></dt> |
| 80 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 81 | <td><nobr><b><tt id='l2h-244' xml:id='l2h-244' class="method">__mod__</tt></b>(</nobr></td> |
| 82 | <td><var>self, other</var>)</td></tr></table></dt> |
| 83 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 84 | <td><nobr><b><tt id='l2h-245' xml:id='l2h-245' class="method">__divmod__</tt></b>(</nobr></td> |
| 85 | <td><var>self, other</var>)</td></tr></table></dt> |
| 86 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 87 | <td><nobr><b><tt id='l2h-246' xml:id='l2h-246' class="method">__pow__</tt></b>(</nobr></td> |
| 88 | <td><var>self, other</var><big>[</big><var>, modulo</var><big>]</big><var></var>)</td></tr></table></dt> |
| 89 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 90 | <td><nobr><b><tt id='l2h-247' xml:id='l2h-247' class="method">__lshift__</tt></b>(</nobr></td> |
| 91 | <td><var>self, other</var>)</td></tr></table></dt> |
| 92 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 93 | <td><nobr><b><tt id='l2h-248' xml:id='l2h-248' class="method">__rshift__</tt></b>(</nobr></td> |
| 94 | <td><var>self, other</var>)</td></tr></table></dt> |
| 95 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 96 | <td><nobr><b><tt id='l2h-249' xml:id='l2h-249' class="method">__and__</tt></b>(</nobr></td> |
| 97 | <td><var>self, other</var>)</td></tr></table></dt> |
| 98 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 99 | <td><nobr><b><tt id='l2h-250' xml:id='l2h-250' class="method">__xor__</tt></b>(</nobr></td> |
| 100 | <td><var>self, other</var>)</td></tr></table></dt> |
| 101 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 102 | <td><nobr><b><tt id='l2h-251' xml:id='l2h-251' class="method">__or__</tt></b>(</nobr></td> |
| 103 | <td><var>self, other</var>)</td></tr></table></dt> |
| 104 | <dd>These methods are |
| 105 | called to implement the binary arithmetic operations (<code>+</code>, |
| 106 | <code>-</code>, <code>*</code>, <code>//</code>, <code>%</code>, |
| 107 | <tt class="function">divmod()</tt><a id='l2h-252' xml:id='l2h-252'></a>, |
| 108 | <tt class="function">pow()</tt><a id='l2h-253' xml:id='l2h-253'></a>, <code>**</code>, <code><</code><code><</code>, |
| 109 | <code>></code><code>></code>, <code>&</code>, <code>^</code>, <code>|</code>). For instance, to |
| 110 | evaluate the expression <var>x</var><code>+</code><var>y</var>, where <var>x</var> is an |
| 111 | instance of a class that has an <tt class="method">__add__()</tt> method, |
| 112 | <code><var>x</var>.__add__(<var>y</var>)</code> is called. The <tt class="method">__divmod__()</tt> |
| 113 | method should be the equivalent to using <tt class="method">__floordiv__()</tt> and |
| 114 | <tt class="method">__mod__()</tt>; it should not be related to <tt class="method">__truediv__()</tt> |
| 115 | (described below). Note that |
| 116 | <tt class="method">__pow__()</tt> should be defined to accept an optional third |
| 117 | argument if the ternary version of the built-in |
| 118 | <tt class="function">pow()</tt><a id='l2h-254' xml:id='l2h-254'></a> function is to be supported. |
| 119 | </dl> |
| 120 | |
| 121 | <P> |
| 122 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 123 | <td><nobr><b><tt id='l2h-255' xml:id='l2h-255' class="method">__div__</tt></b>(</nobr></td> |
| 124 | <td><var>self, other</var>)</td></tr></table></dt> |
| 125 | <dd> |
| 126 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 127 | <td><nobr><b><tt id='l2h-256' xml:id='l2h-256' class="method">__truediv__</tt></b>(</nobr></td> |
| 128 | <td><var>self, other</var>)</td></tr></table></dt> |
| 129 | <dd>The division operator (<code>/</code>) is implemented by these methods. The |
| 130 | <tt class="method">__truediv__()</tt> method is used when <code>__future__.division</code> |
| 131 | is in effect, otherwise <tt class="method">__div__()</tt> is used. If only one of |
| 132 | these two methods is defined, the object will not support division in |
| 133 | the alternate context; <tt class="exception">TypeError</tt> will be raised instead. |
| 134 | </dl> |
| 135 | |
| 136 | <P> |
| 137 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 138 | <td><nobr><b><tt id='l2h-257' xml:id='l2h-257' class="method">__radd__</tt></b>(</nobr></td> |
| 139 | <td><var>self, other</var>)</td></tr></table></dt> |
| 140 | <dd> |
| 141 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 142 | <td><nobr><b><tt id='l2h-258' xml:id='l2h-258' class="method">__rsub__</tt></b>(</nobr></td> |
| 143 | <td><var>self, other</var>)</td></tr></table></dt> |
| 144 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 145 | <td><nobr><b><tt id='l2h-259' xml:id='l2h-259' class="method">__rmul__</tt></b>(</nobr></td> |
| 146 | <td><var>self, other</var>)</td></tr></table></dt> |
| 147 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 148 | <td><nobr><b><tt id='l2h-260' xml:id='l2h-260' class="method">__rdiv__</tt></b>(</nobr></td> |
| 149 | <td><var>self, other</var>)</td></tr></table></dt> |
| 150 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 151 | <td><nobr><b><tt id='l2h-261' xml:id='l2h-261' class="method">__rtruediv__</tt></b>(</nobr></td> |
| 152 | <td><var>self, other</var>)</td></tr></table></dt> |
| 153 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 154 | <td><nobr><b><tt id='l2h-262' xml:id='l2h-262' class="method">__rfloordiv__</tt></b>(</nobr></td> |
| 155 | <td><var>self, other</var>)</td></tr></table></dt> |
| 156 | <dd> |
| 157 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 158 | <td><nobr><b><tt id='l2h-263' xml:id='l2h-263' class="method">__rmod__</tt></b>(</nobr></td> |
| 159 | <td><var>self, other</var>)</td></tr></table></dt> |
| 160 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 161 | <td><nobr><b><tt id='l2h-264' xml:id='l2h-264' class="method">__rdivmod__</tt></b>(</nobr></td> |
| 162 | <td><var>self, other</var>)</td></tr></table></dt> |
| 163 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 164 | <td><nobr><b><tt id='l2h-265' xml:id='l2h-265' class="method">__rpow__</tt></b>(</nobr></td> |
| 165 | <td><var>self, other</var>)</td></tr></table></dt> |
| 166 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 167 | <td><nobr><b><tt id='l2h-266' xml:id='l2h-266' class="method">__rlshift__</tt></b>(</nobr></td> |
| 168 | <td><var>self, other</var>)</td></tr></table></dt> |
| 169 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 170 | <td><nobr><b><tt id='l2h-267' xml:id='l2h-267' class="method">__rrshift__</tt></b>(</nobr></td> |
| 171 | <td><var>self, other</var>)</td></tr></table></dt> |
| 172 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 173 | <td><nobr><b><tt id='l2h-268' xml:id='l2h-268' class="method">__rand__</tt></b>(</nobr></td> |
| 174 | <td><var>self, other</var>)</td></tr></table></dt> |
| 175 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 176 | <td><nobr><b><tt id='l2h-269' xml:id='l2h-269' class="method">__rxor__</tt></b>(</nobr></td> |
| 177 | <td><var>self, other</var>)</td></tr></table></dt> |
| 178 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 179 | <td><nobr><b><tt id='l2h-270' xml:id='l2h-270' class="method">__ror__</tt></b>(</nobr></td> |
| 180 | <td><var>self, other</var>)</td></tr></table></dt> |
| 181 | <dd>These methods are |
| 182 | called to implement the binary arithmetic operations (<code>+</code>, |
| 183 | <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>, |
| 184 | <tt class="function">divmod()</tt><a id='l2h-271' xml:id='l2h-271'></a>, |
| 185 | <tt class="function">pow()</tt><a id='l2h-272' xml:id='l2h-272'></a>, <code>**</code>, <code><</code><code><</code>, |
| 186 | <code>></code><code>></code>, <code>&</code>, <code>^</code>, <code>|</code>) with reflected |
| 187 | (swapped) operands. These functions are only called if the left |
| 188 | operand does not support the corresponding operation. For instance, |
| 189 | to evaluate the expression <var>x</var><code>-</code><var>y</var>, where <var>y</var> is an |
| 190 | instance of a class that has an <tt class="method">__rsub__()</tt> method, |
| 191 | <code><var>y</var>.__rsub__(<var>x</var>)</code> is called. Note that ternary |
| 192 | <tt class="function">pow()</tt><a id='l2h-273' xml:id='l2h-273'></a> will not try calling |
| 193 | <tt class="method">__rpow__()</tt> (the coercion rules would become too |
| 194 | complicated). |
| 195 | </dl> |
| 196 | |
| 197 | <P> |
| 198 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 199 | <td><nobr><b><tt id='l2h-274' xml:id='l2h-274' class="method">__iadd__</tt></b>(</nobr></td> |
| 200 | <td><var>self, other</var>)</td></tr></table></dt> |
| 201 | <dd> |
| 202 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 203 | <td><nobr><b><tt id='l2h-275' xml:id='l2h-275' class="method">__isub__</tt></b>(</nobr></td> |
| 204 | <td><var>self, other</var>)</td></tr></table></dt> |
| 205 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 206 | <td><nobr><b><tt id='l2h-276' xml:id='l2h-276' class="method">__imul__</tt></b>(</nobr></td> |
| 207 | <td><var>self, other</var>)</td></tr></table></dt> |
| 208 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 209 | <td><nobr><b><tt id='l2h-277' xml:id='l2h-277' class="method">__idiv__</tt></b>(</nobr></td> |
| 210 | <td><var>self, other</var>)</td></tr></table></dt> |
| 211 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 212 | <td><nobr><b><tt id='l2h-278' xml:id='l2h-278' class="method">__itruediv__</tt></b>(</nobr></td> |
| 213 | <td><var>self, other</var>)</td></tr></table></dt> |
| 214 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 215 | <td><nobr><b><tt id='l2h-279' xml:id='l2h-279' class="method">__ifloordiv__</tt></b>(</nobr></td> |
| 216 | <td><var>self, other</var>)</td></tr></table></dt> |
| 217 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 218 | <td><nobr><b><tt id='l2h-280' xml:id='l2h-280' class="method">__imod__</tt></b>(</nobr></td> |
| 219 | <td><var>self, other</var>)</td></tr></table></dt> |
| 220 | <dd> |
| 221 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 222 | <td><nobr><b><tt id='l2h-281' xml:id='l2h-281' class="method">__ipow__</tt></b>(</nobr></td> |
| 223 | <td><var>self, other</var><big>[</big><var>, modulo</var><big>]</big><var></var>)</td></tr></table></dt> |
| 224 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 225 | <td><nobr><b><tt id='l2h-282' xml:id='l2h-282' class="method">__ilshift__</tt></b>(</nobr></td> |
| 226 | <td><var>self, other</var>)</td></tr></table></dt> |
| 227 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 228 | <td><nobr><b><tt id='l2h-283' xml:id='l2h-283' class="method">__irshift__</tt></b>(</nobr></td> |
| 229 | <td><var>self, other</var>)</td></tr></table></dt> |
| 230 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 231 | <td><nobr><b><tt id='l2h-284' xml:id='l2h-284' class="method">__iand__</tt></b>(</nobr></td> |
| 232 | <td><var>self, other</var>)</td></tr></table></dt> |
| 233 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 234 | <td><nobr><b><tt id='l2h-285' xml:id='l2h-285' class="method">__ixor__</tt></b>(</nobr></td> |
| 235 | <td><var>self, other</var>)</td></tr></table></dt> |
| 236 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 237 | <td><nobr><b><tt id='l2h-286' xml:id='l2h-286' class="method">__ior__</tt></b>(</nobr></td> |
| 238 | <td><var>self, other</var>)</td></tr></table></dt> |
| 239 | <dd>These methods are called to implement the augmented arithmetic |
| 240 | operations (<code>+=</code>, <code>-=</code>, <code>*=</code>, <code>/=</code>, <code>%=</code>, |
| 241 | <code>**=</code>, <code><</code><code><=</code>, <code>></code><code>>=</code>, <code>&=</code>, |
| 242 | <code>^=</code>, <code>|=</code>). These methods should attempt to do the |
| 243 | operation in-place (modifying <var>self</var>) and return the result (which |
| 244 | could be, but does not have to be, <var>self</var>). If a specific method |
| 245 | is not defined, the augmented operation falls back to the normal |
| 246 | methods. For instance, to evaluate the expression |
| 247 | <var>x</var><code>+=</code><var>y</var>, where <var>x</var> is an instance of a class that |
| 248 | has an <tt class="method">__iadd__()</tt> method, <code><var>x</var>.__iadd__(<var>y</var>)</code> is |
| 249 | called. If <var>x</var> is an instance of a class that does not define a |
| 250 | <tt class="method">__iadd__()</tt> method, <code><var>x</var>.__add__(<var>y</var>)</code> and |
| 251 | <code><var>y</var>.__radd__(<var>x</var>)</code> are considered, as with the |
| 252 | evaluation of <var>x</var><code>+</code><var>y</var>. |
| 253 | </dl> |
| 254 | |
| 255 | <P> |
| 256 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 257 | <td><nobr><b><tt id='l2h-287' xml:id='l2h-287' class="method">__neg__</tt></b>(</nobr></td> |
| 258 | <td><var>self</var>)</td></tr></table></dt> |
| 259 | <dd> |
| 260 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 261 | <td><nobr><b><tt id='l2h-288' xml:id='l2h-288' class="method">__pos__</tt></b>(</nobr></td> |
| 262 | <td><var>self</var>)</td></tr></table></dt> |
| 263 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 264 | <td><nobr><b><tt id='l2h-289' xml:id='l2h-289' class="method">__abs__</tt></b>(</nobr></td> |
| 265 | <td><var>self</var>)</td></tr></table></dt> |
| 266 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 267 | <td><nobr><b><tt id='l2h-290' xml:id='l2h-290' class="method">__invert__</tt></b>(</nobr></td> |
| 268 | <td><var>self</var>)</td></tr></table></dt> |
| 269 | <dd>Called to implement the unary arithmetic operations (<code>-</code>, |
| 270 | <code>+</code>, <tt class="function">abs()</tt><a id='l2h-291' xml:id='l2h-291'></a> and <code>~</code>). |
| 271 | </dl> |
| 272 | |
| 273 | <P> |
| 274 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 275 | <td><nobr><b><tt id='l2h-292' xml:id='l2h-292' class="method">__complex__</tt></b>(</nobr></td> |
| 276 | <td><var>self</var>)</td></tr></table></dt> |
| 277 | <dd> |
| 278 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 279 | <td><nobr><b><tt id='l2h-293' xml:id='l2h-293' class="method">__int__</tt></b>(</nobr></td> |
| 280 | <td><var>self</var>)</td></tr></table></dt> |
| 281 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 282 | <td><nobr><b><tt id='l2h-294' xml:id='l2h-294' class="method">__long__</tt></b>(</nobr></td> |
| 283 | <td><var>self</var>)</td></tr></table></dt> |
| 284 | <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 285 | <td><nobr><b><tt id='l2h-295' xml:id='l2h-295' class="method">__float__</tt></b>(</nobr></td> |
| 286 | <td><var>self</var>)</td></tr></table></dt> |
| 287 | <dd>Called to implement the built-in functions |
| 288 | <tt class="function">complex()</tt><a id='l2h-296' xml:id='l2h-296'></a>, |
| 289 | <tt class="function">int()</tt><a id='l2h-297' xml:id='l2h-297'></a>, <tt class="function">long()</tt><a id='l2h-298' xml:id='l2h-298'></a>, |
| 290 | and <tt class="function">float()</tt><a id='l2h-299' xml:id='l2h-299'></a>. Should return a value of |
| 291 | the appropriate type. |
| 292 | </dl> |
| 293 | |
| 294 | <P> |
| 295 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 296 | <td><nobr><b><tt id='l2h-300' xml:id='l2h-300' class="method">__oct__</tt></b>(</nobr></td> |
| 297 | <td><var>self</var>)</td></tr></table></dt> |
| 298 | <dd> |
| 299 | <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 300 | <td><nobr><b><tt id='l2h-301' xml:id='l2h-301' class="method">__hex__</tt></b>(</nobr></td> |
| 301 | <td><var>self</var>)</td></tr></table></dt> |
| 302 | <dd>Called to implement the built-in functions |
| 303 | <tt class="function">oct()</tt><a id='l2h-302' xml:id='l2h-302'></a> and |
| 304 | <tt class="function">hex()</tt><a id='l2h-303' xml:id='l2h-303'></a>. Should return a string value. |
| 305 | </dl> |
| 306 | |
| 307 | <P> |
| 308 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 309 | <td><nobr><b><tt id='l2h-304' xml:id='l2h-304' class="method">__coerce__</tt></b>(</nobr></td> |
| 310 | <td><var>self, other</var>)</td></tr></table></dt> |
| 311 | <dd> |
| 312 | Called to implement ``mixed-mode'' numeric arithmetic. Should either |
| 313 | return a 2-tuple containing <var>self</var> and <var>other</var> converted to |
| 314 | a common numeric type, or <code>None</code> if conversion is impossible. When |
| 315 | the common type would be the type of <code>other</code>, it is sufficient to |
| 316 | return <code>None</code>, since the interpreter will also ask the other |
| 317 | object to attempt a coercion (but sometimes, if the implementation of |
| 318 | the other type cannot be changed, it is useful to do the conversion to |
| 319 | the other type here). A return value of <code>NotImplemented</code> is |
| 320 | equivalent to returning <code>None</code>. |
| 321 | </dl> |
| 322 | |
| 323 | <P> |
| 324 | |
| 325 | <DIV CLASS="navigation"> |
| 326 | <div class='online-navigation'> |
| 327 | <p></p><hr /> |
| 328 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 329 | <tr> |
| 330 | <td class='online-navigation'><a rel="prev" title="3.3.6 Additional methods for" |
| 331 | href="sequence-methods.html"><img src='../icons/previous.png' |
| 332 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 333 | <td class='online-navigation'><a rel="parent" title="3.3 Special method names" |
| 334 | href="specialnames.html"><img src='../icons/up.png' |
| 335 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 336 | <td class='online-navigation'><a rel="next" title="3.3.8 Coercion rules" |
| 337 | href="coercion-rules.html"><img src='../icons/next.png' |
| 338 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 339 | <td align="center" width="100%">Python Reference Manual</td> |
| 340 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 341 | href="contents.html"><img src='../icons/contents.png' |
| 342 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 343 | <td class='online-navigation'><img src='../icons/blank.png' |
| 344 | border='0' height='32' alt='' width='32' /></td> |
| 345 | <td class='online-navigation'><a rel="index" title="Index" |
| 346 | href="genindex.html"><img src='../icons/index.png' |
| 347 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 348 | </tr></table> |
| 349 | <div class='online-navigation'> |
| 350 | <b class="navlabel">Previous:</b> |
| 351 | <a class="sectref" rel="prev" href="sequence-methods.html">3.3.6 Additional methods for</A> |
| 352 | <b class="navlabel">Up:</b> |
| 353 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> |
| 354 | <b class="navlabel">Next:</b> |
| 355 | <a class="sectref" rel="next" href="coercion-rules.html">3.3.8 Coercion rules</A> |
| 356 | </div> |
| 357 | </div> |
| 358 | <hr /> |
| 359 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 360 | </DIV> |
| 361 | <!--End of Navigation Panel--> |
| 362 | <ADDRESS> |
| 363 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 364 | </ADDRESS> |
| 365 | </BODY> |
| 366 | </HTML> |