Does "equal" in Python use == or is?

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


 

Recently, a reader asked me an interview question, hoping that I could write down the relevant content in detail, no, arrange it now!

The difference between is and ==

It seems very simple, but there are also many details. Today's article is to take you to an in-depth understanding of related knowledge.

Give a few examples

If you explain it at the beginning, then many friends will not understand it, so let's run a few programs first.

>>> x = y = [4,3,1]
>>> z = [4,3,1]
>>> print(x is y)
True
>>> print(x == y)
True
>>> print(x == z)
True
>>> print(x is z)
False

First, let’s take a look at this example. We will find that x returns True regardless of whether is or == is used, but the contents of x and z are the same. Use == to return True, but use is to return False.

In Python, == compares values, and is compares objects.

Among them, the Python object mainly contains id, value and data type. In the comparison of is, the id is compared.

So here we print the id values ​​of x, y, and z

>>> print(id(x))
140513337973408
>>> print(id(y))
140513337973408
>>> print(id(z))
140513337973248

By comparing the id values ​​of x and y are equal, but the id value of z is different.

Why are x and y the same?

In Python, = is not only an assignment, it also assigns the reference address, so in memory, x and y call the same object.

You can refer to the picture below

 

 

Let's continue watching

>>> a = '123'
>>> b = '123'
>>> print(a is b)
True
>>> print(a == b)
True
##############
>>> c = 100
>>> d = 100
>>> print(c == d)
True
>>> print(c is d)
True
##############
>>> a = 1000
>>> b = 1000
>>> print(a is b)
False
>>> print(a==b)
True

Some friends may be confused when they see this. What is this...?

First of all, let me talk about why strings are equal. In fact, there is an intern mechanism in python. In this mechanism, only one string object with the same value will be saved. This is to improve the running efficiency of Python.

So how is it achieved? In fact, it's very simple. This intern mechanism works by maintaining a string storage pool (a dictionary). The key of this dictionary is the value of the string, and the value is the reference address of the string.

Every time you create a new string, you will go to the string pool to find out whether there is the same string value, and if it exists, you will directly call the reference address of the string.

Believe that you see this, you already understand why strings are always equal.

 

 

Next, we will continue to talk about the comparison of the following two integer types, which confuses us even more.

>>> c = 100
>>> d = 100
>>> print(c == d)
True
>>> print(c is d)
True
##############
>>> a = 1000
>>> b = 1000
>>> print(a is b)
False
>>> print(a==b)
True

In fact, it is not difficult to understand. In python, there is the concept of a small integer object pool. The small integer object pool is simply the objects created in the interval [-5,256] and will not be recycled.

In other words, when you create an integer in the range of [-5,256], it will first check whether there is the same value, and if there is, it will directly quote the address.

If what you create is not in this range, then it will re-create a new object, so its address must be different.

Summary: In Python, only small integer object pools and strings will call existing addresses, and other objects like list, tuple, and dict will recreate a new object.

to sum up

In Python, there is still some difference between is and ==, we can't use it arbitrarily.

But now we write code basically on the IDE, such as pycharm, in pycharm it optimizes the interpreter, no matter whether your value is in the range [-5,256], as long as the value is equal, the two of them will be completely equal. So here needs special attention.

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112911497