Home > front end >  Why it shows different results while I use id() comment for numpy element?
Why it shows different results while I use id() comment for numpy element?

Time:09-21

I'm a newbie for python. I used this below code to make array, and I checked the memory location. It showed different results. I don't know why. I thought both would show same results. Do you know why? If so please, let me know the reason. thanks in advance.

a = np.array([[0,1,2],[3,4,5],[6,7,8]]) 
id(a[0][0]) #1301373082608
id(a[0][0]) #1301373082704

CodePudding user response:

a is more than just a simple wrapper around the nested list you passed as an argument. a[0] constructs a new 3x1 array from a each time it is used, and a[0][0] constructs a new int64 value from that 3x1 array. Beyond that, you can't predict if each object will have different id numbers, or if (since their lifetimes do not overlap) Python will reuse the same id for each.

  • Related