After reading this and this, which are pretty similar to my question, I still cannot understand the following behaviour:
a = 257
b = 257
print(a is b) #False
a, b = 257, 257
print(a is b) #True
When printing id(a)
and id(b)
I can see that the variables, to which the values were assigned in separate lines, have different ids, whereas with multiple assignment both values have the same id:
a = 257
b = 257
print(id(a)) #139828809414512
print(id(b)) #139828809414224
a, b = 257, 257
print(id(a)) #139828809414416
print(id(b)) #139828809414416
But it's impossible to explain this behaviour by saying that multiple assignment of same values always creates pointers to the same id since:
a, b = -1000, -1000
print(id(a)) #139828809414448
print(id(b)) #139828809414288
Is there a clear rule, which explains when the variables get the same id
and when not?
edit
relevant info: The code in this question was run in interactive mode(ipython3)
[id(i) for i in (1000,1000,1000,1000)]
:-) – Hestia257
s were the same object, there would be a simple, sensible pattern to it. – Spaniard