Difference between is and = in Python, and difference between pythonis

Source: Internet
Author: User

Difference between is and = in Python, and difference between pythonis

In Python, if two objects (variables) are equal, you can use the "is" and "=" operations. But what is the difference between them? When is "is" and when is "=" used "? During the interview, it was difficult for many candidates to fully clarify the two. Therefore, in this article, the "Python Zen" will give a brief comparison of the two.

Here is an example.

Mr. Huang has recently purchased a P90D Tesla, which is named "P" for the moment ", this car is exactly the same as the old Wang's car (the name of the car is "Xiao Wang"). Both models, appearances, and prices are the same. Here we can say that "John" and "John" are two identical and equal (euqal), but essentially they are two different objects. One day, Mr. Smith gave his car another name named "". When we say "P", we are actually talking about "", because essentially two names refer to the same object, here we call "Small P" and "Love Ju" completely equal (identical ).

In Python, the difference between "=" and "is" can be analogous to this example. The former is an equality comparison, and the comparison is whether the values of the two objects are equal, the latter is a consistent comparison, comparing whether the memory space addresses of the two objects are the same.

Obviously, if the memory address is the same, their values are certainly the same. Therefore, if "is" returns True, then "=" will certainly return True, but vice versa.

Talk is cheap, show me the code

First, create a list object, specify a name for it, and then define another variable B to point it to the same object.

>>> a = [1, 2, 3]>>> b = a

The printed values of a and B are equal, because these two variables point to the same object, just like giving a car two different names.

>>> a[1, 2, 3]>>> b[1, 2, 3]

Of course, True is and = are returned.

>>> a == bTrue>>> a is bTrue

Create a new object. Although the values are the same, they are essentially two different objects in two different Memory Spaces. Therefore, "is" returns False.

>>> c = [1,2,3]>>> a is cFalse

"Is" returns True only when the two variables to be compared point to the same object, and "=" ultimately depends on the _ eq _ () method of the object, essentially, two variables perform the "=" comparison operation to call the _ eq _ () method of the object. For example:

>>> class Foo(object):    def __eq__(self, other):      return True>>> f = Foo()>>> f == 1True>>> f == NoneTrue>>> f is NoneFalse

Because the eq method of the custom class Foo always returns True, "=" with any object returns True. It is different from None, so the 'is' operation returns False.

Finally, let's think about why the same operation has different results.

>>> a = 257>>> b = 257>>> a is bFalse>>> a = 123>>> b = 123>>> a is bTrue

Summary:

If you want to compare whether two values are the same, use =. If you want to compare whether two values are the same, use is.

In fact, objects in python are similar to pointers in C. Only pointers with the same address are the same pointers.

Related Article

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.