The difference between is and == in Python

  • 2020-05-27 06:07:14
  • OfStack

In Python, you can compare the equality of two objects (variables) using "is" and "==", but what is the difference between them? When to use "is" and when to use "=="? In the interview, many candidates find it difficult to fully explain the two, so in this article, "zen of Python" will introduce the two in a simple and profound way.

Let me give you an example

Recently, xiao huang had a lot of money and bought an P90D tesla. We will call it "little P" for the moment. This car and the car of Lao wang's family next door (named "xiao wang") are of the same model and the same price, and they are produced in the same batch. Here we can say that "little P" and" xiao wang "are two identical, 1-mode, 1-like vehicles (euqal), but essentially they are two different objects. When we say "little P", we are really talking about "little P", because essentially both names refer to the same object. Here we call "little P" and "little identical" the exact equivalent (identical).

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

The & # 8203; Obviously, if the memory address is the same, then their value must be the same, so if "is" returns True, then "==" 1 must also return True, and vice versa.

talk is cheap, show me the code

Create a list object, assign it a name a, and define another variable b to point to the same object.


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

a and b print the same value because the two variables point to the same object, which is like giving a car two different names.


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

Of course, both is and == return True.


>>> a == b
True
>>> a is b
True

Create a new object. Although the value is 1, they are essentially two different objects in two different memory Spaces, so "is" returns False.


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

Only "is" returns True if only the two variables of comparison point to the same object, and "==" ultimately depends on the object's s s 53en__ () method. Essentially, it is the object s s 54en__ () method that is called with the two variables' s s s s == comparison operation. Such as:


>>> class Foo(object):
    def __eq__(self, other):
      return True

>>> f = Foo()
>>> f == 1
True
>>> f == None
True
>>> f is None
False

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

Finally, please think about this code, why the same operation will have different results


>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = 123
>>> b = 123
>>> a is b
True

Conclusion:

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

In fact, the object is in python compares is very similar to the pointer in C language, only the pointer with the same address is the same pointer.


Related articles: