Chris
Chris

Reputation: 1033

Why np.resize() out_place, while ndarray.resize() in_place?

According to my understanding, with classes instance.method(parameters)=class.method(instance,parameters), so it's just a notation difference. But np.resize(ndarray) changes out_place, while ndarray.resize() changes in_place.

What am I missing?

Upvotes: 6

Views: 111

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95948

Yes, but numpy isn't the class, it is the module. You want numpy.ndarray as the class. Observe:

In [1]: import numpy as np

In [2]: arr = np.array([1,2,3])

In [3]: np.ndarray.resize(arr, (3,1))

In [4]: arr
Out[4]:
array([[1],
       [2],
       [3]])
In [5]: np.ndarray.resize(arr, (3,))

In [6]: arr
Out[6]: array([1, 2, 3])

So, numpy.resize is just a module-level function, that returns a new array instead of modifying the array in-place.

Upvotes: 3

Related Questions