numbers
index
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/numbers.py
Module Docs

Abstract Base Classes (ABCs) for numbers, according to PEP 3141.
 
TODO: Fill out more detailed documentation on the operators.

 
Classes
       
__builtin__.object
Number
Complex
Real
Rational
Integral

 
class Complex(Number)
    Complex defines the operations that work on the builtin complex type.
 
In short, those are: a conversion to complex, .real, .imag, +, -,
*, /, abs(), .conjugate, ==, and !=.
 
If it is given heterogenous arguments, and doesn't have special
knowledge about them, it should fall back to the builtin complex
type as described below.
 
 
Method resolution order:
Complex
Number
__builtin__.object

Methods defined here:
__abs__(self)
Returns the Real distance from 0. Called for abs(self).
__add__(self, other)
self + other
__complex__(self)
Return a builtin complex instance. Called for complex(self).
__div__(self, other)
self / other without __future__ division
 
May promote to float.
__eq__(self, other)
self == other
__mul__(self, other)
self * other
__ne__(self, other)
self != other
__neg__(self)
-self
__nonzero__(self)
True if self != 0. Called for bool(self).
__pos__(self)
+self
__pow__(self, exponent)
self**exponent; should promote to float or complex when necessary.
__radd__(self, other)
other + self
__rdiv__(self, other)
other / self without __future__ division
__rmul__(self, other)
other * self
__rpow__(self, base)
base ** self
__rsub__(self, other)
other - self
__rtruediv__(self, other)
other / self with __future__ division
__sub__(self, other)
self - other
__truediv__(self, other)
self / other with __future__ division.
 
Should promote to float when necessary.
conjugate(self)
(x+y*i).conjugate() returns (x-y*i).

Data descriptors defined here:
imag
Retrieve the imaginary component of this number.
 
This should subclass Real.
real
Retrieve the real component of this number.
 
This should subclass Real.

Data and other attributes defined here:
__abstractmethods__ = frozenset(['__abs__', '__add__', '__complex__', '__div__', '__eq__', '__mul__', ...])

Data and other attributes inherited from Number:
__hash__ = None
__metaclass__ = <class 'abc.ABCMeta'>
Metaclass for defining Abstract Base Classes (ABCs).
 
Use this metaclass to create an ABC.  An ABC can be subclassed
directly, and then acts as a mix-in class.  You can also register
unrelated concrete classes (even built-in classes) and unrelated
ABCs as 'virtual subclasses' -- these and their descendants will
be considered subclasses of the registering ABC by the built-in
issubclass() function, but the registering ABC won't show up in
their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable (not
even via super()).

 
class Integral(Rational)
    Integral adds a conversion to long and the bit-string operations.
 
 
Method resolution order:
Integral
Rational
Real
Complex
Number
__builtin__.object

Methods defined here:
__and__(self, other)
self & other
__float__(self)
float(self) == float(long(self))
__index__(self)
Called whenever an index is needed, such as in slicing
__invert__(self)
~self
__long__(self)
long(self)
__lshift__(self, other)
self << other
__or__(self, other)
self | other
__pow__(self, exponent, modulus=None)
self ** exponent % modulus, but maybe faster.
 
Accept the modulus argument if you want to support the
3-argument version of pow(). Raise a TypeError if exponent < 0
or any argument isn't Integral. Otherwise, just implement the
2-argument version described in Complex.
__rand__(self, other)
other & self
__rlshift__(self, other)
other << self
__ror__(self, other)
other | self
__rrshift__(self, other)
other >> self
__rshift__(self, other)
self >> other
__rxor__(self, other)
other ^ self
__xor__(self, other)
self ^ other

Data descriptors defined here:
denominator
Integers have a denominator of 1.
numerator
Integers are their own numerators.

Data and other attributes defined here:
__abstractmethods__ = frozenset(['__abs__', '__add__', '__and__', '__div__', '__eq__', '__floordiv__', ...])

Methods inherited from Real:
__complex__(self)
complex(self) == complex(float(self), 0)
__divmod__(self, other)
divmod(self, other): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
__floordiv__(self, other)
self // other: The floor() of self/other.
__le__(self, other)
self <= other
__lt__(self, other)
self < other
 
< on Reals defines a total ordering, except perhaps for NaN.
__mod__(self, other)
self % other
__rdivmod__(self, other)
divmod(other, self): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
__rfloordiv__(self, other)
other // self: The floor() of other/self.
__rmod__(self, other)
other % self
__trunc__(self)
trunc(self): Truncates self to an Integral.
 
Returns an Integral i such that:
  * i>0 iff self>0;
  * abs(i) <= abs(self);
  * for any Integral j satisfying the first two conditions,
    abs(i) >= abs(j) [i.e. i has "maximal" abs among those].
i.e. "truncate towards 0".
conjugate(self)
Conjugate is a no-op for Reals.

Data descriptors inherited from Real:
imag
Real numbers have no imaginary component.
real
Real numbers are their real component.

Methods inherited from Complex:
__abs__(self)
Returns the Real distance from 0. Called for abs(self).
__add__(self, other)
self + other
__div__(self, other)
self / other without __future__ division
 
May promote to float.
__eq__(self, other)
self == other
__mul__(self, other)
self * other
__ne__(self, other)
self != other
__neg__(self)
-self
__nonzero__(self)
True if self != 0. Called for bool(self).
__pos__(self)
+self
__radd__(self, other)
other + self
__rdiv__(self, other)
other / self without __future__ division
__rmul__(self, other)
other * self
__rpow__(self, base)
base ** self
__rsub__(self, other)
other - self
__rtruediv__(self, other)
other / self with __future__ division
__sub__(self, other)
self - other
__truediv__(self, other)
self / other with __future__ division.
 
Should promote to float when necessary.

Data and other attributes inherited from Number:
__hash__ = None
__metaclass__ = <class 'abc.ABCMeta'>
Metaclass for defining Abstract Base Classes (ABCs).
 
Use this metaclass to create an ABC.  An ABC can be subclassed
directly, and then acts as a mix-in class.  You can also register
unrelated concrete classes (even built-in classes) and unrelated
ABCs as 'virtual subclasses' -- these and their descendants will
be considered subclasses of the registering ABC by the built-in
issubclass() function, but the registering ABC won't show up in
their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable (not
even via super()).

 
class Number(__builtin__.object)
    All numbers inherit from this class.
 
If you just want to check if an argument x is a number, without
caring what kind, use isinstance(x, Number).
 
  Data and other attributes defined here:
__abstractmethods__ = frozenset([])
__hash__ = None
__metaclass__ = <class 'abc.ABCMeta'>
Metaclass for defining Abstract Base Classes (ABCs).
 
Use this metaclass to create an ABC.  An ABC can be subclassed
directly, and then acts as a mix-in class.  You can also register
unrelated concrete classes (even built-in classes) and unrelated
ABCs as 'virtual subclasses' -- these and their descendants will
be considered subclasses of the registering ABC by the built-in
issubclass() function, but the registering ABC won't show up in
their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable (not
even via super()).

 
class Rational(Real)
    .numerator and .denominator should be in lowest terms.
 
 
Method resolution order:
Rational
Real
Complex
Number
__builtin__.object

Methods defined here:
__float__(self)
float(self) = self.numerator / self.denominator
 
It's important that this conversion use the integer's "true"
division rather than casting one side to float before dividing
so that ratios of huge integers convert without overflowing.

Data descriptors defined here:
denominator
numerator

Data and other attributes defined here:
__abstractmethods__ = frozenset(['__abs__', '__add__', '__div__', '__eq__', '__floordiv__', '__le__', ...])

Methods inherited from Real:
__complex__(self)
complex(self) == complex(float(self), 0)
__divmod__(self, other)
divmod(self, other): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
__floordiv__(self, other)
self // other: The floor() of self/other.
__le__(self, other)
self <= other
__lt__(self, other)
self < other
 
< on Reals defines a total ordering, except perhaps for NaN.
__mod__(self, other)
self % other
__rdivmod__(self, other)
divmod(other, self): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
__rfloordiv__(self, other)
other // self: The floor() of other/self.
__rmod__(self, other)
other % self
__trunc__(self)
trunc(self): Truncates self to an Integral.
 
Returns an Integral i such that:
  * i>0 iff self>0;
  * abs(i) <= abs(self);
  * for any Integral j satisfying the first two conditions,
    abs(i) >= abs(j) [i.e. i has "maximal" abs among those].
i.e. "truncate towards 0".
conjugate(self)
Conjugate is a no-op for Reals.

Data descriptors inherited from Real:
imag
Real numbers have no imaginary component.
real
Real numbers are their real component.

Methods inherited from Complex:
__abs__(self)
Returns the Real distance from 0. Called for abs(self).
__add__(self, other)
self + other
__div__(self, other)
self / other without __future__ division
 
May promote to float.
__eq__(self, other)
self == other
__mul__(self, other)
self * other
__ne__(self, other)
self != other
__neg__(self)
-self
__nonzero__(self)
True if self != 0. Called for bool(self).
__pos__(self)
+self
__pow__(self, exponent)
self**exponent; should promote to float or complex when necessary.
__radd__(self, other)
other + self
__rdiv__(self, other)
other / self without __future__ division
__rmul__(self, other)
other * self
__rpow__(self, base)
base ** self
__rsub__(self, other)
other - self
__rtruediv__(self, other)
other / self with __future__ division
__sub__(self, other)
self - other
__truediv__(self, other)
self / other with __future__ division.
 
Should promote to float when necessary.

Data and other attributes inherited from Number:
__hash__ = None
__metaclass__ = <class 'abc.ABCMeta'>
Metaclass for defining Abstract Base Classes (ABCs).
 
Use this metaclass to create an ABC.  An ABC can be subclassed
directly, and then acts as a mix-in class.  You can also register
unrelated concrete classes (even built-in classes) and unrelated
ABCs as 'virtual subclasses' -- these and their descendants will
be considered subclasses of the registering ABC by the built-in
issubclass() function, but the registering ABC won't show up in
their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable (not
even via super()).

 
class Real(Complex)
    To ComplexReal adds the operations that work on real numbers.
 
In short, those are: a conversion to float, trunc(), divmod,
%, <, <=, >, and >=.
 
Real also provides defaults for the derived operations.
 
 
Method resolution order:
Real
Complex
Number
__builtin__.object

Methods defined here:
__complex__(self)
complex(self) == complex(float(self), 0)
__divmod__(self, other)
divmod(self, other): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
__float__(self)
Any Real can be converted to a native float object.
 
Called for float(self).
__floordiv__(self, other)
self // other: The floor() of self/other.
__le__(self, other)
self <= other
__lt__(self, other)
self < other
 
< on Reals defines a total ordering, except perhaps for NaN.
__mod__(self, other)
self % other
__rdivmod__(self, other)
divmod(other, self): The pair (self // other, self % other).
 
Sometimes this can be computed faster than the pair of
operations.
__rfloordiv__(self, other)
other // self: The floor() of other/self.
__rmod__(self, other)
other % self
__trunc__(self)
trunc(self): Truncates self to an Integral.
 
Returns an Integral i such that:
  * i>0 iff self>0;
  * abs(i) <= abs(self);
  * for any Integral j satisfying the first two conditions,
    abs(i) >= abs(j) [i.e. i has "maximal" abs among those].
i.e. "truncate towards 0".
conjugate(self)
Conjugate is a no-op for Reals.

Data descriptors defined here:
imag
Real numbers have no imaginary component.
real
Real numbers are their real component.

Data and other attributes defined here:
__abstractmethods__ = frozenset(['__abs__', '__add__', '__div__', '__eq__', '__float__', '__floordiv__', ...])

Methods inherited from Complex:
__abs__(self)
Returns the Real distance from 0. Called for abs(self).
__add__(self, other)
self + other
__div__(self, other)
self / other without __future__ division
 
May promote to float.
__eq__(self, other)
self == other
__mul__(self, other)
self * other
__ne__(self, other)
self != other
__neg__(self)
-self
__nonzero__(self)
True if self != 0. Called for bool(self).
__pos__(self)
+self
__pow__(self, exponent)
self**exponent; should promote to float or complex when necessary.
__radd__(self, other)
other + self
__rdiv__(self, other)
other / self without __future__ division
__rmul__(self, other)
other * self
__rpow__(self, base)
base ** self
__rsub__(self, other)
other - self
__rtruediv__(self, other)
other / self with __future__ division
__sub__(self, other)
self - other
__truediv__(self, other)
self / other with __future__ division.
 
Should promote to float when necessary.

Data and other attributes inherited from Number:
__hash__ = None
__metaclass__ = <class 'abc.ABCMeta'>
Metaclass for defining Abstract Base Classes (ABCs).
 
Use this metaclass to create an ABC.  An ABC can be subclassed
directly, and then acts as a mix-in class.  You can also register
unrelated concrete classes (even built-in classes) and unrelated
ABCs as 'virtual subclasses' -- these and their descendants will
be considered subclasses of the registering ABC by the built-in
issubclass() function, but the registering ABC won't show up in
their MRO (Method Resolution Order) nor will method
implementations defined by the registering ABC be callable (not
even via super()).

 
Data
        __all__ = ['Number', 'Complex', 'Real', 'Rational', 'Integral']