In a Python class (OOP) methods which operate in place (modify self
or its attributes) are acceptable, and if anything, more common than ones that return a new object. That's also true for built in classes like dict
or list
.
For example in numpy
we often recommend the list append
approach to building an new array:
In [296]: alist = []
In [297]: for i in range(3):
...: alist.append(i)
...:
In [298]: alist
Out[298]: [0, 1, 2]
This is common enough that we can readily write it as a list comprehension:
In [299]: [i for i in range(3)]
Out[299]: [0, 1, 2]
alist.sort
operates in-place, sorted(alist)
returns a new list.
In numpy
methods that return a new array are much more common. In fact sort
is about the only in-place method I can think of off hand. That and a direct modification of shape
: arr.shape=(...)
.
A number of basic numpy operations return a view
. That shares data memory with the source, but the array object wrapper is new. In fact even indexing an element returns a new object.
So while you ultimately need to check the documentation, it's usually safe to assume a numpy function or method returns a new object, as opposed to operating in-place.
More often users are confused by the numpy functions that have the same name as a method. In most of those cases the function makes sure the argument(s) is an array, and then delegates the action to its method. Also keep in mind that in Python operators are translated into method calls - +
to __add__
, [index]
to __getitem__()
etc. +=
is a kind of in-place operation.