1

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)

Aivengo
  • 23
  • 4
  • The way your question is written it is hard to understand what you want. Try and explain a bit more clearly – stelioslogothetis Jul 11 '17 at 13:26
  • 1
    Using `is` in this way is not meaningful. Do you mean `==` rather than `is` ? – cdarke Jul 11 '17 at 13:31
  • @cdarke no, I mean is. Id (False) == id (False) have the same values. I am interested in the values of id during the execution of is. Because the values in memory are different. – Aivengo Jul 11 '17 at 13:56
  • But that does not compare the values of the id's, it compares the references of the id's, not their values. – cdarke Jul 11 '17 at 18:50

2 Answers2

1

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.

jasonharper
  • 9,450
  • 2
  • 18
  • 42
1

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

cdarke
  • 42,728
  • 8
  • 80
  • 84