After reading the questions here and here, it seems that the OWNDATA
flag isn't always reliable for numpy arrays in determining whether an object is a copy of another one or not.
The answers to these questions seem to say however that OWNDATA
sometimes produces 'false negatives' (edit: on second thought, that might be more of a 'false positive') i.e. answers 'false' when in fact an object is a copy, and can be safely changed without changing the original.
Now I'm wondering: is the following a case of the flag also sometimes yielding false positives, i.e. claiming something is a copy, when it really isn't? (Question, part 1) Alternatively, I misunderstand what the OWNDATA
flag is intended to tell me...
a = np.arange(3)
b = a
print(b.flags['OWNDATA']) # True
b = b+1
print(a==b) # False => b is copy, matching `OWNDATA` flag
b = a
print(b.flags['OWNDATA']) # True
b += 1
print(a==b) # True => b not a copy, mismatch with `OWNDATA`?
(Question, part 2) Finally: if neither OWNDATA
, or a.base is b
are reliable indicators to tell whether an object is a copy or not, then what what is the right to determine it? The questions linked above mention may_share_memory
, but that one seems to be overeager in the other direction, answering 'True' on anything that is not constructed or created as a an explicit np.copy of another object.