Python-is, = =, CMP ()

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.