'Is it a good way to use numpy.ndarray.strides attribute like this?

While browsing through attributes, I found that I can change np.ndarray.strides attribute explicitly like so:

arr = np.array([8, 3, 4], dtype=np.uint16)
arr.strides = 1
arr
>>> array([  8, 768,   3], dtype=uint16)

I know intuitively this is not safe way to assign an attribute explicitly (might be antipattern). Despite that, I know very well, what it is supposed to do.

Another way to get my expected result is:

np.lib.stride_tricks.as_strided(arr, shape=(3,), strides=(1,))

I'm looking for any guidelines about patterns of programming that helps to understand, should I avoid setting attributes like this.

Also, is there a safer way in numpy to assign a new value to .strides attribute?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source