This article introduces the id () function in python. For more information, see >>> a = 2.5.
>>> B = 2.5
>>> C = B
>>> A is c
False
>>> A = 2
>>> B = 2
>>> C = B
>>> A is c
True
When using the is function, print the numbers a and B are assigned 2.5 and 2 respectively, and find that:
>>> A = 2
>>> B = 2
>>> Id ()
21132060
>>> Id (B)
21132060
>>> A = 2.5
>>> B = 2.5
>>> Id ()
19622112
>>> Id (B)
29321464
When a and B are 2, the id is the same, but 2.5 is different. this problem also occurs when the string is short, when B is assigned a very short string, their IDs are the same, but long strings do not;
I have read the following articles:
Http://stackoverflow.com/questions/4293408/ids-of-immutable-types
Http://stackoverflow.com/questions/3402679/identifying-objects-why-does-the-returned-value-from-id-change
Then, we can get a simple conclusion: the interpreter made a small optimization for int and short strings with a small value, and assigned only one object to make them have the same id.