Open
Description
This section of NEP 13 describes how __array_ufunc__
interacts with Python's binary operations:
http://www.numpy.org/neps/nep-0013-ufunc-overrides.html#behavior-in-combination-with-python-s-binary-operations
But it doesn't fully describe what happens an object of unknown type that implements arithmetic (e.g., decimal.Decimal
) but doesn't implement any NumPy special methods like __array_ufunc__
or __array_priority__
is encountered in arithmetic with a NumPy array:
- Assuming the unknown type properly returns
NotImplemented
from binary operations for unrecognized arguments, the numpy ufunc will get called. - The NumPy ufunc (e.g.,
np.add
) will coerce the unknown object into a scalar NumPy array with object dtype. - The scalar array gets broadcast to the same shape as the NumPy array.
- The NumPy ufunc loop (for object dtype) calls the Python operator on each element of the two arrays.
- The result is another object dtype array.
If this seems correct, I will make a minor addendum to add this clarification. This is particularly valuable as an example to other projects (e.g., see pandas-dev/pandas#23293) of the right way to implement arithmetic like NumPy.
Metadata
Metadata
Assignees
Labels
No labels