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?
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?
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.
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.
For the information on id() you can refer to What is the id( ) function used for?.