View vs Copy (NumPy)
A view is a new array object that shares the same underlying data as the original, so writing to one changes the other; a copy is independent data. Knowing which an operation returns is key to avoiding subtle bugs.
NumPy
Many NumPy operations return views to save memory and time: basic slicing, reshape, transpose, and ravel typically produce a view into the original buffer. Because the data is shared, modifying the view modifies the original — a feature for performance but a trap if you didn't expect it.
Other operations always return copies: fancy (integer) indexing, boolean indexing, and the explicit .copy() method allocate fresh memory, so changes to the result leave the original untouched. The .flatten() method copies while .ravel() prefers a view, which is the main difference between them.
When in doubt, check arr.base — a view's base points back to the array it shares data with, while a copy's base is None. If you need to mutate a slice safely, call .copy() first.
Syntax
view = a[1:4] # shares data; copy = a[1:4].copy() # independentExamples
Python — Writing to the slice changed the original.
import numpy as np
arr = np.arange(5)
v = arr[1:4]
v[0] = 99
print(arr)Output:
[ 0 99 2 3 4]