Trait cpython::NumberProtocol

source ·
pub trait NumberProtocol: ObjectProtocol {
Show 22 methods // Provided methods fn add(&self, py: Python<'_>, other: impl ToPyObject) -> PyResult<PyObject> { ... } fn subtract( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn multiply( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn matrix_multiply( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn power( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn power_modulo( &self, py: Python<'_>, exp: impl ToPyObject, z: impl ToPyObject ) -> PyResult<PyObject> { ... } fn true_divide( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn floor_divide( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn modulo( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn div_mod( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn negative(&self, py: Python<'_>) -> PyResult<PyObject> { ... } fn positive(&self, py: Python<'_>) -> PyResult<PyObject> { ... } fn absolute(&self, py: Python<'_>) -> PyResult<PyObject> { ... } fn bitwise_invert(&self, py: Python<'_>) -> PyResult<PyObject> { ... } fn left_shift( &self, py: Python<'_>, bits: impl ToPyObject ) -> PyResult<PyObject> { ... } fn right_shift( &self, py: Python<'_>, bits: impl ToPyObject ) -> PyResult<PyObject> { ... } fn bitwise_and( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn bitwise_xor( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn bitwise_or( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject> { ... } fn to_int(&self, py: Python<'_>) -> PyResult<PyLong> { ... } fn to_float(&self, py: Python<'_>) -> PyResult<PyFloat> { ... } fn to_index(&self, py: Python<'_>) -> PyResult<PyLong> { ... }
}
Expand description

Operations on numeric objects

Provided Methods§

source

fn add(&self, py: Python<'_>, other: impl ToPyObject) -> PyResult<PyObject>

Perform addition (self + other)

Invokes the __add__ magic-method

source

fn subtract(&self, py: Python<'_>, other: impl ToPyObject) -> PyResult<PyObject>

Perform subtraction (self - other)

Invokes the __sub__ magic-method

source

fn multiply(&self, py: Python<'_>, other: impl ToPyObject) -> PyResult<PyObject>

Perform multiplication (self * other)

Invokes the __mul__ magic-method

source

fn matrix_multiply( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject>

Perform matrix multiplication, equivalent to the Python expression self @ other

Invokes the __matmul__ magic-method

This method is only available with Python 3.

See PEP 0456 for details.

source

fn power(&self, py: Python<'_>, other: impl ToPyObject) -> PyResult<PyObject>

Perform exponentiation, equivalent to the Python expression self ** other, or the two-argument form of the builtin method pow: pow(self, other)

Invokes the __pow__ magic-method

See also NumberProtocol::power_modulo.

source

fn power_modulo( &self, py: Python<'_>, exp: impl ToPyObject, z: impl ToPyObject ) -> PyResult<PyObject>

Perform exponentiation modulo an integer, mathematically equivalent to self ** other % mod but computed much more efficiently.

Equivalent to invoking the three-argument form of the builtin pow method: pow(self, other, z)

Invoking with a None for modulo is equivalent to the regular power operation.

Invokes the __pow__ magic-method

source

fn true_divide( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject>

Perform the “true division” operation, equivalent to the Python expression self / other,

Invokes the __truediv__ magic-method.

source

fn floor_divide( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject>

Perform the “floor division” operation, equivalent to the Python expression self // other,

This method was added in Python 3. If compiling against Python 2, it unconditional throws an error.

Invokes the __floordiv__ magic-method.

source

fn modulo(&self, py: Python<'_>, other: impl ToPyObject) -> PyResult<PyObject>

Return the remainder of dividing self by other, equivalent to the Python expression self % other

Invokes the __mod__ magic-method.

source

fn div_mod(&self, py: Python<'_>, other: impl ToPyObject) -> PyResult<PyObject>

Perform combined division and modulo, equivalent to the builtin method divmod(self, other)

Invokes the __divmod__ magic-method.

source

fn negative(&self, py: Python<'_>) -> PyResult<PyObject>

Perform the negation of self (-self)

Invokes the __neg__ magic-method.

source

fn positive(&self, py: Python<'_>) -> PyResult<PyObject>

Invoke the ‘positive’ operation, equivalent to the Python expression +self

Invokes the __pos__ magic-method

source

fn absolute(&self, py: Python<'_>) -> PyResult<PyObject>

Return the absolute value of self, equivalent to calling the builtin function abs

Invokes the __abs__ magic-method.

source

fn bitwise_invert(&self, py: Python<'_>) -> PyResult<PyObject>

Perform the bitwise negation of self, equivalent to the Python expression ~self

Invokes the __invert__ magic-method

source

fn left_shift( &self, py: Python<'_>, bits: impl ToPyObject ) -> PyResult<PyObject>

Shift this value to the left by the specified number of bits, equivalent to the Python expression self << bits

Invokes the __lshift__ magic-method

source

fn right_shift( &self, py: Python<'_>, bits: impl ToPyObject ) -> PyResult<PyObject>

Shift this value to the right by the specified number of bits, equivalent to the Python expression self >> bits

Invokes the __rshift__ magic-method

source

fn bitwise_and( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject>

Perform the “bitwise and” of self & other

Invokes the __and__ magic-method.

source

fn bitwise_xor( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject>

Perform the “bitwise exclusive or”, equivalent to Python expression self ^ other

Invokes the __xor__ magic-method.

source

fn bitwise_or( &self, py: Python<'_>, other: impl ToPyObject ) -> PyResult<PyObject>

Perform the “bitwise or” of self | other

Invokes the __or__ magic-method.

source

fn to_int(&self, py: Python<'_>) -> PyResult<PyLong>

Convert this object to an integer, equivalent to the builtin function int(self)

Invokes the __int__ magic-method.

Throws an exception if unable to perform the conversion.

source

fn to_float(&self, py: Python<'_>) -> PyResult<PyFloat>

Convert this object to a float, equivalent to the builtin function float(self)

Invokes the __float__ magic-method.

Throws an exception if unable to perform the conversion.

source

fn to_index(&self, py: Python<'_>) -> PyResult<PyLong>

Losslessly convert this object to an integer index, as if calling operator.index()

The presence of this method indicates this object is an integer type.

Calls the __index__ magic-method.

See also: Documentation on the corresponding magic-method

Object Safety§

This trait is not object safe.

Implementors§