next up previous contents index
Next: Execution model Up: Special method names Previous: Special methods for

Special methods for numeric types

__add__(self, other)
__sub__(self, other)
__mul__(self, other)
__div__(self, other)
__mod__(self, other)
__divmod__(self, other)
__pow__(self, other)
__lshift__(self, other)
__rshift__(self, other)
__and__(self, other)
__xor__(self, other)
__or__(self, other)
Called to implement the binary arithmetic operations (+, -, *, /, %, divmod(), pow(), <<, >>, &, ^, |).                        

__neg__(self)
__pos__(self)
__abs__(self)
__invert__(self)
Called to implement the unary arithmetic operations (-, +, abs() and ~).        

__nonzero__(self)
Called to implement boolean testing; should return 0 or 1. An alternative name for this method is __len__.  

__coerce__(self, other)
Called to implement ``mixed-mode'' numeric arithmetic. Should either return a tuple containing self and other converted to a common numeric type, or None if no way of conversion is known. When the common type would be the type of other, it is sufficient to return None, 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).  

Note that this method is not called to coerce the arguments to + and *, because these are also used to implement sequence concatenation and repetition, respectively. Also note that, for the same reason, in n*x, where n is a built-in number and x is an instance, a call to x.__mul__(n) is made. gif  

__int__(self)
__long__(self)
__float__(self)
Called to implement the built-in functions int(), long() and float(). Should return a value of the appropriate type.      

__oct__(self)
__hex__(self)
Called to implement the built-in functions oct() and hex(). Should return a string value.    



next up previous contents index
Next: Execution model Up: Special method names Previous: Special methods for



guido@cwi.nl