numpy icon indicating copy to clipboard operation
numpy copied to clipboard

DOC: Incorrect description of in-place operations

Open ghost opened this issue 8 months ago • 2 comments

Issue with current documentation:

https://numpy.org/devdocs/reference/generated/numpy.ndarray.iadd.html

In the documentation for in-place operations (__iadd__, __isub__, etc.), it states "Return self+=value." However, an in-place operation (self+=value) is a statement, not an expression, so it does not have a return value.

Idea or request for content:

The description should be same as self+=value, perform self+=value or something similar.

ghost avatar Apr 20 '25 02:04 ghost

The docstring is indeed not very clear, but the __iadd__ operator does actually return an array; it is just that value is added in-place and then self is returned:

In [2]: a = np.arange(5.0)

In [3]: a.__iadd__(1)
Out[3]: array([1., 2., 3., 4., 5.])

In [4]: a
Out[4]: array([1., 2., 3., 4., 5.])

mhvk avatar Apr 20 '25 15:04 mhvk

What I am pointing out is not whether __iadd__ returns a value, but whether it is appropriate to express the return value as self+=value. At least in the Python language, return self+=value is not syntactically correct.

ghost avatar Apr 20 '25 23:04 ghost