0

I thought 'is' operator would return True if the two variables are pointing the same object. (not just value)
In the example below,

a = np.array([1,2,3,4])
b = a[1:3]
b[0] = 11

then a[1] value will be changed to 11.
And b array will be pointing to the a[1:3].

So I thought 'b[0] is a[1]' would return True. (b[0] and a[1] are referencing the same object) But that line returned False.

Could you help me to understand why the result is False?

Thanks

daniel
  • 1
  • 1
  • 1
    What programming language is this? Please update the tags. – Raymond Chen Apr 16 '22 at 00:31
  • Youi wasted five tags that weren't needed, and left off the one that actually was, which is the language you're using. Please [edit] your post, remove all 5 tags you have now, and add just the one for that language. – Ken White Apr 16 '22 at 00:51
  • They're not the same object. They're two different objects referencing different parts of the same underlying memory (underlying memory that isn't a Python level object itself, so it can never be identity tested), but the *objects themselves* are different (they *have* to be; the same object *obviously* can't refer to the whole array *and* just elements 1 and 2 *at the same time*). – ShadowRanger Apr 16 '22 at 02:59
  • @ShadowRanger: The `is` test was on `b[0]` and `a[1]`, not `b` and `a`. – user2357112 Apr 16 '22 at 03:07
  • It will help if you read intro docs that explain how numpy stores and accesses arrays. It's not the same as lists. – hpaulj Apr 16 '22 at 06:38

0 Answers0