I am comparing two elements of a numpy array. The memory address obtained by id()
function for both elements are different. Also the is
operator gives out that the two elements are not same.
However if I compare memory address of the two array elements using ==
operator it gives out that the two elements are same.
I am not able to understand how the ==
operator gives output as True
when the two memory address are different.
Below is my code.
import numpy as np
a = np.arange(8)
newarray = a[np.array([3,4,2])]
print("Initial array : ", a)
print("New array : ", newarray)
# comparison of two element using 'is' operator
print("\ncomparison using is operator : ",a[3] is newarray[0])
# comparison of memory address of two element using '==' operator
print("comparison using == opertor : ", id(a[3]) == id(newarray[0]))
# memory address of both elements of array
print("\nMemory address of a : ", id(a[3]))
print("Memory address of newarray : ", id(newarray[0]))
Output:
Initial array : [0 1 2 3 4 5 6 7]
New array : [3 4 2]
comparison using is
operator : False
comparison using ==
operator : True
Memory address of a : 2807046101296
Memory address of newarray : 2808566470576