is mainly to determine whether 2 variables refer to the same object, if so, return true, otherwise false.
- Determine the number equality do not use the IS operator
| 123456789101112 |
>>> a = 256>>> b = 256>>> id(a)9987148>>> id(b)9987148>>> a = 257>>> b = 257>>> id(a)11662816>>> id(b)11662828 |
Why two times is return a different result? Isn't it supposed to be true?
Because string pooling (or called intern). is equals the ID of two objects (which, from the bottom, can be considered a reference to the same piece of memory). As to why "ABC" was intern and "a BC" did not, this is the Python parser implementation decision, may change.
- = = is used to determine whether the values of two objects are equal (unlike Java, in Java = = is used to determine whether the same object).
- The CMP () function is the equivalent of <,==,> but in Python3, the CMP () function is removed, so I will avoid using this function sparingly.
| 1234567 |
>>> a = ' abc ' >>> b = ' abc ' >>> a is b true >>> id (a) = = id (b) true >>> |
It can be seen that a string of the same content is actually the same object (a string directly assigned in Java can also be judged by = =, but objects instantiated with new will need to be judged using equals (string s).)
Python-is, = =, CMP ()