I know this has been asked like a million times. Somehow am not convinced with the answers(pl forgive me). I have written the following sample code.
class Equals:
def checkEquals(self, element1, element2):
return element1 == element2
def checkIs(self,element1,element2):
return element1 is element2
e = Equals()
x = 1000
y = 1000
print x
print y
print id(x)
print id(y)
print e.checkequals(x,y)
print e.checkis(x,y)
Here is the output.
1000
1000
37695976
37695976
True
True
Now my questions are.
Though x and y values are same, Shouldnt they be placed in different memory locations unless x = y = 1000? Why are they placed in the same location? I believe thats why x is y returns True.
And in the same program if I give x = [10,20] , y = [10,20] both have different memory locations. Why is that so?
I have observed the same concept in java as well for "==" as they are comparing the memory location of the objects. Thats the reason I had to revisit this in python as well.