Python "is" and "= ="

Source: Internet
Author: User

Python compares two objects for equality in two ways, and simply, they differ as follows:

1,is is a comparison of two references that point to the same object (reference comparison)

2,== is a comparison of the values of two objects for equality.

>>>a = [2,3,4]>>>b = a is atrue#  Tile operation reassigned Object  is Afalse # The values are equal True    

Implementation principle
is to compare whether the two are the same object, compared with memory addresses (ID is equal).

= = is a value comparison, an immutable object, such as INT,STR, which directly compares values. For Python-known objects, the __sq__ function is called to compare.

Object caching mechanism

Python will be relatively small object cache, the next time you use, go back to the buffer to find, if found, do not go back to open up new memory, but continue to assign the address of the small object to the new value

1 c = 12 d = 13print is d)4True5 6  is 10**37False8 -= = 10**39 True

The computed assignment does not use a buffer.

Python "is" and "= ="

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.