If you assign an immutable object to a variable in python, the variable as well as the value points to the same location,
>>> a = 5 >>> b = 5 >>> id(5) 11372376 >>> id(a) 11372376 >>> id(b) 11372376 >>> a == b True >>> a is b True
Comparing identifiers will work exactly as you can see the ID values. Now try assigning variable objects to python variables.
>>> x = '123 4' >>> y = '123 4' >>> x == y True >>> x is y False >>> id(x) 21598832 >>> id(y) 21599408 >>> id('123 4') 21599312
Here you can see the difference in identifiers. how "is" compares values ββwith an address, where "==" compares directly with a reference value. However, it does not give an error in the case of immutable objects, since all points are in one place, but in the case of variability, since values ββcan change, variables are pointed to the current object and, therefore, give you a false result.
Hope this helps :)
binu.py
source share