0
p=0
i=0
print(id(p) is id(i))

As p and i share common memory location, Still the output of this program is False.

Kindly anyone let me know why I am getting False as the id of both p and i is same?

  • Possible duplicate of [Is there a difference between "==" and "is"?](https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is) – Patrick Artner Oct 09 '19 at 07:21
  • Integers are "identical" objects in the range of -5 to 256 - the output of `id()` is an integer NOT inside that range - so the calls to `id(p)` and `id(i)` are different objects hence not True when compared with `is` – Patrick Artner Oct 09 '19 at 07:23

3 Answers3

0

p & i may refer to the same object. But id(p) & id(i) are different objects.

p=0
i=0
print(id(p))
print(id(i))
print(id(p) is id(i))
print(id(p) == id(i))

Output:

4458376944
4458376944
False
True

The integers with value 4458376944 that the id function creates are separate objects. Specifically this is because those numbers are out of the interning range for python: "is" operator behaves unexpectedly with integers

To compare two id() values you should just use the == operator instead.

rdas
  • 20,604
  • 6
  • 33
  • 46
0

try using this

print(id(p) == id(0))

The == operator compares the values of both the operands and checks for value equality. Whereas is operator checks whether both the operands refer to the same object or not.

ritvika
  • 1
  • 3
-1

For the information on id() you can refer to What is the id( ) function used for?.

Kevin Ng
  • 2,146
  • 1
  • 13
  • 18