5

This is a series question about the cache mechanism of Python.

Firstly, I read Why (0-6) is -6 = False? to find there's something called pool of integer objects in Python, then I tried some other cases and found a phenomenon out of my understanding

In : a = 257; b = 257; id(a) == id(b)
Out: False

In : a, b = 257, 257; id(a) == id(b)
Out: True

In : a, b = (1,), (1,); id(a) == id(b)
Out: False

As the answer to that question mentioned, 257 is not in the range of small integer, so there should be a new object when you assigned a new variable with the value of 257, but a,b = 257, 257 showed True. So I was assuming this kind of assigning method would assign them two same IDs at an initializing period, but the third case proved this assumption wrong.

Could you please explain why this way of assigning has different behaviors with non-cached integers(not in [-5,256]) and tuples?

Community
  • 1
  • 1
Pedro
  • 51
  • 3
  • 1
    Tuples have nothing to do with integer caching; I'm not sure why they're part of the question. – TigerhawkT3 Nov 22 '16 at 03:12
  • 1
    @TigerhawkT3 But doesn't integer caching only exist in small int? I am not asking about integer caching actually, the question is about the internal mechanism when you use `a, b = value, value` – Pedro Nov 22 '16 at 03:25
  • 1
    No, I can't explain it, except to say - it doesn't matter! You're worrying about implementation details that have no effect on the proper operation of your program. – Mark Ransom Nov 22 '16 at 03:29
  • i try the 1st example, isn't its suppose to return True ? – Skycc Nov 22 '16 at 05:12
  • @Skycc Sorry, I didn't tell the environment I run it on. I used ipython2 and ipython3 and found both returned False. But the built-in interactive python environments returned True. – Pedro Nov 22 '16 at 08:41

0 Answers0