Complete worked example
python
import numpy as np
def inspect_example(values):
data = np.array(values, dtype=np.int64, copy=True)
if data.ndim != 1 or data.size == 0:
raise ValueError("expected a non-empty vector")
selected = data[:2].copy()
selected += 10
return {"original": data.tolist(), "selected": selected.tolist(),
"shared": bool(np.shares_memory(data, selected))}- Reasoning
- The worked function selects the first two values with a basic slice followed by copy. It adds ten to the selected buffer and returns the unchanged original, the changed selection and the result of shares_memory. That boolean supports a concrete relationship check rather than relying on Python identity or on how the arrays print. The tests include a one-element vector because a valid slice can be shorter than the requested stop. Advanced indexing with a list of integer positions produces an independent selection as well. However, an assignment directly to data[indexes] writes into data; it is not equivalent to mutating a separately stored selection. Explain the full expression before inferring ownership.