
If a,b=500,500 a is b# True (a and b they have same memory location) But, c,d=Id(a),id(b) c is d # False Id of a and Id of b is same but this statement return False Rahul Podder

Hi Rahul, On Fri, Oct 6, 2023, at 3:09 PM, Rahul Podder wrote:
If a,b=500,500 a is b# True (a and b they have same memory location) But, c,d=Id(a),id(b) c is d # False Id of a and Id of b is same but this statement return False
You should not be using `is` to do value comparisons. You should be using `==` which would give you the result you're after. The slightly longer story is that CPython (the implementation I assume you're using) will intern (pre-create) a bunch of integer instances in a certain range. 500 falls in that range and as such there will only be one instance of the integer with the value 500, this makes the `is` (identity) comparison return true. `id(500)` however returns a much larger integer; which is not interned and as such multiple instances of it will exist and the identity comparison fails. If you were to write this out with literals you're doing `500 is 500` -> `True` `140517430945640 is 140517430945640` -> `False` If you were to write it that way Python will also warn you: `SyntaxWarning: "is" with a literal. Did you mean "=="?` Regards, Simon
participants (2)
-
Rahul Podder
-
Simon de Vlieger