What does (numpy) __array_wrap__ do?
talks about ndarray
subclasses and hooks like __array_wrap__
. np.array
takes copy
parameter, forcing the result to be a copy, even if it isn't required by other considerations. ravel
returns a view, flatten
a copy. So it is probably possible, and maybe not too difficult, to construct a ndarray
subclass that forces a copy. It may involve modifying a hook like __array_wrap__
.
Or maybe modifying the .__getitem__
method. Indexing as in slice_of_arr = arr[0:6]
involves a call to __getitem__
. For ndarray
this is compiled, but for a masked array, it is python code that you could use as an example:
/usr/lib/python3/dist-packages/numpy/ma/core.py
It may be something as simple as
def __getitem__(self, indx):
"""x.__getitem__(y) <==> x[y]
"""
# _data = ndarray.view(self, ndarray) # change to:
_data = ndarray.copy(self, ndarray)
dout = ndarray.__getitem__(_data, indx)
return dout
But I suspect that by the time you develop and fully test such a subclass, you might fall in love with the default no-copy approach. While this view-v-copy business bites many new comers (especially if coming from MATLAB), I haven't seen complaints from experienced users. Look at other numpy SO questions; you won't see a lot copy()
calls.
Even regular Python users are used asking themselves whether a reference or slice is a copy or not, and whether something is mutable or not.
for example with lists:
In [754]: ll=[1,2,[3,4,5],6]
In [755]: llslice=ll[1:-1]
In [756]: llslice[1][1:2]=[10,11,12]
In [757]: ll
Out[757]: [1, 2, [3, 10, 11, 12, 5], 6]
modifying an item an item inside a slice modifies that same item in the original list. In contrast to numpy
, a list slice is a copy. But it's a shallow copy. You have to take extra effort to make a deep copy (import copy
).
/usr/lib/python3/dist-packages/numpy/lib/index_tricks.py
contains some indexing functions aimed at making certain indexing operations more convenient. Several are actually classes, or class instances, with custom __getitem__
methods. They may also serve as models of how to customize your slicing and indexing.