Python | Numpy numpy.ndarray.__isub__()

Python | Numpy numpy.ndarray.__isub__()

The numpy.ndarray.__isub__() method is an implementation of the "in-place subtraction" operation (using the -= operator) for numpy arrays. In Python, special methods that start and end with double underscores (like __isub__) are called "dunder" methods (short for "double underscore"). These methods often correspond to particular operators or built-in functions.

In the case of numpy.ndarray.__isub__(), it corresponds to the in-place subtraction (-=) operation.

Here's a quick example to show how it works:

import numpy as np # Creating two numpy arrays arr1 = np.array([1, 2, 3, 4, 5]) arr2 = np.array([5, 4, 3, 2, 1]) # Using in-place subtraction arr1 -= arr2 print(arr1) 

Output:

[-4 -2 0 2 4] 

Note that after performing the in-place subtraction, arr1 is modified while arr2 remains unchanged.

Even though you typically won't call the __isub__() method directly (you'd more commonly use the -= operator as shown above), it's still possible to do so. Here's how you might invoke it explicitly:

arr1.__isub__(arr2) 

However, this is not idiomatic and would be considered unusual in typical Python code.


More Tags

android-autofill-manager pdf non-printing-characters unlink progress-bar invoke keypress google-api sublimetext autoconf

More Programming Guides

Other Guides

More Programming Examples