Reputation: 23
Getting the value of the first id is obvious. How to get the value of the second id in the function is ?
id(False)
=> 140399586313184
id(id(False))
=> 140399542553456
id(False) is id(False)
=> False
Python documentation:
operator.is_(a, b)
Return a is b. Tests object identity.
Operator.is_ (a, b) performs the function id(False) two times. The values in the id(False) memory are different. I want to know B when id is running id(False) is id(False)
Upvotes: 1
Views: 55
Reputation: 44364
id (False) is id (False)
compares the references of the id's that are returned by the id()
function. It does not compare the references of False
.
You can get similar effects with any large integers in python, not just id's.
In the case of the C implementation these are large integers (memory addresses) and their actual value is not useful and are implementation specific.
Whether two integers with the same value have the same reference is likewise implementation specific, and not guaranteed. The C implementation does some optimisation, but only of "small" numbers. See "is" operator behaves unexpectedly with integers
Upvotes: 1
Reputation: 9597
is
is NOT a valid way of comparing integers (or most types, for that matter). Both sides of id(False) is id(False)
produce the same integer value, but they are two distinct integer objects (since the value is outside of the range where the small integer cache applies). is
therefore properly returns False. If you had compared the values using ==
, the result would have been True.
Upvotes: 1