<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <link rel="STYLESHEET" href="ref.css" type='text/css' /> <link rel="SHORTCUT ICON" href="../icons/pyfav.gif" /> <link rel='start' href='../index.html' title='Python Documentation Index' /> <link rel="first" href="ref.html" title='Python Reference Manual' /> <link rel='contents' href='contents.html' title="Contents" /> <link rel='index' href='genindex.html' title='Index' /> <link rel='last' href='about.html' title='About this document...' /> <link rel='help' href='about.html' title='About this document...' /> <LINK rel="next" href="coercion-rules.html"> <LINK rel="prev" href="sequence-methods.html"> <LINK rel="parent" href="specialnames.html"> <LINK rel="next" href="coercion-rules.html"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name='aesop' content='information' /> <META name="description" content="Emulating numeric types"> <META name="keywords" content="ref"> <META name="resource-type" content="document"> <META name="distribution" content="global"> <title>3.3.7 Emulating numeric types</title> </head> <body> <DIV CLASS="navigation"> <div id='top-navigation-panel'> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="3.3.6 Additional methods for" href="sequence-methods.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="3.3 Special method names" href="specialnames.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="3.3.8 Coercion rules" href="coercion-rules.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Reference Manual</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><img src='../icons/blank.png' border='0' height='32' alt='' width='32' /></td> <td class='online-navigation'><a rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="sequence-methods.html">3.3.6 Additional methods for</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="coercion-rules.html">3.3.8 Coercion rules</A> </div> <hr /></div> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION005370000000000000000"><!--x--></A><A NAME="numeric-types"><!--z--></A> <BR> 3.3.7 Emulating numeric types </H2> <P> The following methods can be defined to emulate numeric objects. Methods corresponding to operations that are not supported by the particular kind of number implemented (e.g., bitwise operations for non-integral numbers) should be left undefined. <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-238' class="method">__add__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-239' class="method">__sub__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-240' class="method">__mul__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-241' class="method">__floordiv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-242' class="method">__mod__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-243' class="method">__divmod__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-244' class="method">__pow__</tt></b>(</nobr></td> <td><var>self, other</var><big>[</big><var>, modulo</var><big>]</big>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-245' class="method">__lshift__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-246' class="method">__rshift__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-247' class="method">__and__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-248' class="method">__xor__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-249' class="method">__or__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd>These methods are called to implement the binary arithmetic operations (<code>+</code>, <code>-</code>, <code>*</code>, <code>//</code>, <code>%</code>, <tt class="function">divmod()</tt><a id='l2h-250'><!--x--></a>, <tt class="function">pow()</tt><a id='l2h-251'><!--x--></a>, <code>**</code>, <code><</code><code><</code>, <code>></code><code>></code>, <code>&</code>, <code>^</code>, <code>|</code>). For instance, to evaluate the expression <var>x</var><code>+</code><var>y</var>, where <var>x</var> is an instance of a class that has an <tt class="method">__add__()</tt> method, <code><var>x</var>.__add__(<var>y</var>)</code> is called. The <tt class="method">__divmod__()</tt> method should be the equivalent to using <tt class="method">__floordiv__()</tt> and <tt class="method">__mod__()</tt>; it should not be related to <tt class="method">__truediv__()</tt> (described below). Note that <tt class="method">__pow__()</tt> should be defined to accept an optional third argument if the ternary version of the built-in <tt class="function">pow()</tt><a id='l2h-252'><!--x--></a> function is to be supported. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-253' class="method">__div__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-254' class="method">__truediv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd>The division operator (<code>/</code>) is implemented by these methods. The <tt class="method">__truediv__()</tt> method is used when <code>__future__.division</code> is in effect, otherwise <tt class="method">__div__()</tt> is used. If only one of these two methods is defined, the object will not support division in the alternate context; <tt class="exception">TypeError</tt> will be raised instead. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-255' class="method">__radd__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-256' class="method">__rsub__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-257' class="method">__rmul__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-258' class="method">__rdiv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-259' class="method">__rtruediv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-260' class="method">__rfloordiv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-261' class="method">__rmod__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-262' class="method">__rdivmod__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-263' class="method">__rpow__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-264' class="method">__rlshift__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-265' class="method">__rrshift__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-266' class="method">__rand__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-267' class="method">__rxor__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-268' class="method">__ror__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd>These methods are called to implement the binary arithmetic operations (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>, <tt class="function">divmod()</tt><a id='l2h-269'><!--x--></a>, <tt class="function">pow()</tt><a id='l2h-270'><!--x--></a>, <code>**</code>, <code><</code><code><</code>, <code>></code><code>></code>, <code>&</code>, <code>^</code>, <code>|</code>) with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation. For instance, to evaluate the expression <var>x</var><code>-</code><var>y</var>, where <var>y</var> is an instance of a class that has an <tt class="method">__rsub__()</tt> method, <code><var>y</var>.__rsub__(<var>x</var>)</code> is called. Note that ternary <tt class="function">pow()</tt><a id='l2h-271'><!--x--></a> will not try calling <tt class="method">__rpow__()</tt> (the coercion rules would become too complicated). </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-272' class="method">__iadd__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-273' class="method">__isub__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-274' class="method">__imul__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-275' class="method">__idiv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-276' class="method">__itruediv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-277' class="method">__ifloordiv__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-278' class="method">__imod__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-279' class="method">__ipow__</tt></b>(</nobr></td> <td><var>self, other</var><big>[</big><var>, modulo</var><big>]</big>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-280' class="method">__ilshift__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-281' class="method">__irshift__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-282' class="method">__iand__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-283' class="method">__ixor__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-284' class="method">__ior__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd>These methods are called to implement the augmented arithmetic operations (<code>+=</code>, <code>-=</code>, <code>*=</code>, <code>/=</code>, <code>%=</code>, <code>**=</code>, <code><</code><code><=</code>, <code>></code><code>>=</code>, <code>&=</code>, <code>^=</code>, <code>|=</code>). These methods should attempt to do the operation in-place (modifying <var>self</var>) and return the result (which could be, but does not have to be, <var>self</var>). If a specific method is not defined, the augmented operation falls back to the normal methods. For instance, to evaluate the expression <var>x</var><code>+=</code><var>y</var>, where <var>x</var> is an instance of a class that has an <tt class="method">__iadd__()</tt> method, <code><var>x</var>.__iadd__(<var>y</var>)</code> is called. If <var>x</var> is an instance of a class that does not define a <tt class="method">__iadd()</tt> method, <code><var>x</var>.__add__(<var>y</var>)</code> and <code><var>y</var>.__radd__(<var>x</var>)</code> are considered, as with the evaluation of <var>x</var><code>+</code><var>y</var>. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-285' class="method">__neg__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-286' class="method">__pos__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-287' class="method">__abs__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-288' class="method">__invert__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd>Called to implement the unary arithmetic operations (<code>-</code>, <code>+</code>, <tt class="function">abs()</tt><a id='l2h-289'><!--x--></a> and <code>~</code>). </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-290' class="method">__complex__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-291' class="method">__int__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-292' class="method">__long__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-293' class="method">__float__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd>Called to implement the built-in functions <tt class="function">complex()</tt><a id='l2h-294'><!--x--></a>, <tt class="function">int()</tt><a id='l2h-295'><!--x--></a>, <tt class="function">long()</tt><a id='l2h-296'><!--x--></a>, and <tt class="function">float()</tt><a id='l2h-297'><!--x--></a>. Should return a value of the appropriate type. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-298' class="method">__oct__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd> <dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-299' class="method">__hex__</tt></b>(</nobr></td> <td><var>self</var>)</td></tr></table></dt> <dd>Called to implement the built-in functions <tt class="function">oct()</tt><a id='l2h-300'><!--x--></a> and <tt class="function">hex()</tt><a id='l2h-301'><!--x--></a>. Should return a string value. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-302' class="method">__coerce__</tt></b>(</nobr></td> <td><var>self, other</var>)</td></tr></table></dt> <dd> Called to implement ``mixed-mode'' numeric arithmetic. Should either return a 2-tuple containing <var>self</var> and <var>other</var> converted to a common numeric type, or <code>None</code> if conversion is impossible. When the common type would be the type of <code>other</code>, it is sufficient to return <code>None</code>, since the interpreter will also ask the other object to attempt a coercion (but sometimes, if the implementation of the other type cannot be changed, it is useful to do the conversion to the other type here). A return value of <code>NotImplemented</code> is equivalent to returning <code>None</code>. </dl> <P> <DIV CLASS="navigation"> <div class='online-navigation'><hr /> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="3.3.6 Additional methods for" rel="prev" title="3.3.6 Additional methods for" href="sequence-methods.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="3.3 Special method names" rel="parent" title="3.3 Special method names" href="specialnames.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="3.3.8 Coercion rules" rel="next" title="3.3.8 Coercion rules" href="coercion-rules.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Reference Manual</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><img src='../icons/blank.png' border='0' height='32' alt='' width='32' /></td> <td class='online-navigation'><a rel="index" title="Index" rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="sequence-methods.html">3.3.6 Additional methods for</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="coercion-rules.html">3.3.8 Coercion rules</A> </div> </div> <hr /> <span class="release-info">Release 2.3.4, documentation updated on May 20, 2004.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>