Home > Backend Development > Python Tutorial > Example analysis of the difference between is and == in Python

Example analysis of the difference between is and == in Python

Y2J
Release: 2017-04-21 16:06:45
Original
1157 people have browsed it

In python, is checks whether two objects are the same object, and == checks whether they are equal. It is very simple to say, let's analyze it through specific examples

In Python, To compare whether two objects (variables) are equal, you can use the "is" and "==" operations, but what is the difference between them? When to use "is" and when to use "=="? During the interview, I found that many candidates had difficulty explaining the two clearly. Therefore, in this article, "The Zen of Python" will provide an in-depth and easy-to-understand comparative introduction to the two.

Let’s take an example first

Xiao Huang has been very wealthy recently and spent a lot of money to buy a P90D Tesla. Let’s temporarily name this car "Little P". This car It is exactly the same as the car owned by Lao Wang next door (the car is named "Xiao Wang"). It has the same model, appearance and price, and is produced in the same batch. Here we can say that "Little P" and "Little Wang" are two identical and equal vehicles (euqal), but essentially they are two different objects. One day, Xiaojun gave his car another online name of "Aiju". When we say "Xiao P", we are actually discussing "Aiju", because essentially the two names refer to the same car. Objects, here we call "Little P" and "Aiju" completely equal (identical).

In Python, the difference between "==" and "is" can be compared to this example. The former is an equality comparison, which compares whether the values ​​​​in two objects are equal, and the latter is a consistency comparison. What is compared is whether the memory space addresses of the two objects are the same.

Obviously, if the memory addresses are the same, then their values ​​must also be the same. Therefore, if "is" returns True, then "==" must also return True, and vice versa.

talk is cheap, show me the code

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

>>> a = [1, 2, 3]
>>> b = a
Copy after login

The values ​​printed by 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]
Copy after login

Of course, is and == both return True.

>>> a == b
True
>>> a is b
True
Copy after login

Create a new object. Although the values ​​​​are the same, they are essentially two different objects and are in two different memory spaces, so "is" returns False.

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

"is" returns True only when the two compared variables point to the same object, and "==" ultimately depends on the eq() method of the object. In essence, the two variables are " === The comparison operation calls the eq() method of the object. For example:

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

>>> f = Foo()
>>> f == 1
True
>>> f == None
True
>>> f is None
False
Copy after login

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

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

>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = 123
>>> b = 123
>>> a is b
True
Copy after login

Summary:

If you want to compare two If the values ​​are the same, use ==. If the comparison is the same object, use is.

In fact, the objects compared by is in Python are very similar to pointers in C language. Only pointers with the same address are the same. pointer.

The above is the detailed content of Example analysis of the difference between is and == in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template