What is the difference between NumPy object methods and NumPy function calls? What is the difference between NumPy object methods and NumPy function calls? numpy numpy

What is the difference between NumPy object methods and NumPy function calls?


There is a notable difference between numpy.sort and ndarray.sort: the former returns a copy of the array, the latter sorts in-place.

For other methods, one can use equivalent functions just as well. The function form will accept array-like collections that can be converted to a NumPy array; this is sometimes convenient. On the other hand, this comes at the expense of a few more checks and function calls, so the method form should be a tiny bit faster. In practice, this is probably negligible; for me the deciding factor is usually that methods take fewer characters to type.

Some mathematical operations are more naturally written as methods: compare np.transpose(A) and A.T, when A is a 2-dimensional array.