A detailed explanation of the operators "= =" and "is" in Python _python

Source: Internet
Author: User

Objective

Before you tell the difference between the two operators of IS and = =, first know the three basic elements contained in the object in Python: ID (identity), Python type () (data type), and value (values). IS and = = are the object of comparison to judge the role, but the object comparison of the contents of the judgment is not the same. Let's see what the specific difference is.

There are two ways to compare two objects in Python, and in simple terms, they differ as follows:

is to compare whether two references point to the same object (reference comparison).

= = is to compare two objects for equality.

>>> a = [1, 2, 3]
>>> b = A
>>> b is a # A reference to B, they actually point to the memory with an object
True 
>&G  t;> b = = A # Of course, their values are equal to
True
>>> b = a[:] # B Gets the part of a through a slice, where the slice operation is reassigned,
>>> B is a # So we're not pointing to the same object.
False
>>> b = = a # But their value is equal to
True

Implementation principle

The is compares whether the two are the same object, so the memory address (ID is the same) is compared.

= = is a value comparison. Immutable objects, such as INT,STR, that are compared directly to values. For objects known to Python, their __eq__ functions are invoked to compare. (In fact, the known object should also be compared through the built-in __eq__ function). For a custom object, if the __EQ__ function is implemented, it is used to compare, and if not implemented, the effect is the same as = =.

Object caching mechanism

Python will cache the smaller objects, the next time you use smaller objects, it will go to the buffer to find, if found, will not open new memory, but continue to assign the address of the small object to the new value. Example:

>>> c = 1
>>> d = 1
>>> print (c is D) 
True
 
>>> 1000 is 10**3
False
>>> 1000 = = 10**3
True

By calculating the resulting assignment, the buffer is not used. As you can see from the first code example.

For strings, you can force a buffer by using the Intern function.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.