I seem to have had an XY problem in this question regarding how to tell if arrays share the same memory. The ways I was checking were wrong and I'm not sure why.
Let's take a few examples
test = np.ones((3,3))
test2 = np.array(test, ndmin = 2, copy = False)
test3 = np.array(test, ndmin = 3, copy = False)
First, let's check if they're sharing memory using .base
test2.base is test
False
test3.base is test
True
So it seems like test3 is sharing data with test but test2 isn't. In fact test2.base is None => True, which I thought meant that it is seperate memory (i.e. a copy).
This impression is reinforced when I check with .flags
test2.flags['OWNDATA']
True
test3.flags['OWNDATA']
False
It seems again like only test3 is sharing data, and test2 is a copy.
But if I check with the python builtin id(...)
id(test)
248896912
id(test2)
248896912
id(test3)
248897352
Now the id (which is supposedly the adress of the object in memory) of test and test2 match but test3 does not, which gives the exact opposite impression from the above methods.
And of course, both of those impressions are wrong because:
test.__array_interface__['data']
(209580688, False)
test2.__array_interface__['data']
(209580688, False)
test3.__array_interface__['data']
(209580688, False)
The actual buffer addresses all match. Indeed:
test[0,0] = 2
test, test2, test3
(array([[ 2., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]]),
array([[ 2., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]]),
array([[[ 2., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]]]))
So if ndarray.base, ndarray.flags['OWNDATA'], and id(ndarray) don't actually tell me if memory is shared, what are they telling me?
np.shares_memory(test, test2)instead of any of these.