Next: Execution model Up: Special method names Previous: Special methods for

Special methods for numeric types

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

__neg__(self)
join
__pos__(self)
join
__abs__(self)
join
__invert__(self)
break 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.

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

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


guido@cwi.nl