So what's happening here?
Nobody
nobody at nowhere.invalid
Fri Jun 5 10:13:56 EDT 2015
On Fri, 05 Jun 2015 13:11:13 +0000, Paul Appleby wrote:
> (I'd have thought that id(a[1]) and id(b[1]) would be the same if they
> were the same element via different "views", but the id's seem to change
> according to rules that I can't fathom.)
First, a[1] and b[1] aren't views, they're scalars.
Second, different views on the same data are different objects, they just
share the same underlying data. Consider the case where the slice doesn't
cover the entire range:
> a = np.array([1,2,3])
> b = a[:2]
> a
array([1, 2, 3])
> b
array([1, 2])
> id(a)
139682716078288
> id(b)
139682716078368
> b[0] = 99
> a
array([99, 2, 3])
> b
array([99, 2])
The case where a slice *does* cover the entire range isn't special; the
resulting view is still a different object.
More information about the Python-list
mailing list