Skip to content

[Python] Tips: The Difference Between "==" and "is"

In Python, many people will confuse the usages of == and is. In fact, the usage between them is very similar.

For example:

>>> a = 5
>>> b = 5
>>> a == b
True



It seems to be of course, right? What about the use of is?

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



There is the same result! This has also led many people to think that == and is are the same usage, but they are written differently. But it's not like this.

>>> a = 1000
>>> b = 1000
>>> a == b
True
>>> a is b
False



Why is this? Here we need to mention the id() function in Python a little bit.


What is id() function?

In CPython, id() function is a function to view the memory address of object, for example:

>>> target = 100
>>> id(target)
10917664



We can imagine that every variable has its own "variable name", "value", "memory address"… and so on. And used for different attributions is the point == and is differ.


Difference Of "==" and "is"

Let's take a look at the program just now and add id() to view the memory address.

>>> a = 1000
>>> b = 1000

>>> id(a)
140042294421360
>>> id(b)
140042294420272

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



So we can see the biggest difference between == and is:

  • == is to judge whether the "value" between two variables is the same
  • is It is to judge whether the "memory address" between the two variables is the same.

But in this way, how to explain the initial code?

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



The return value of == and is are the same?

This is because in Python, the values from -5 to 256 are all fixed memory addresses. You can directly open two terminals and look at them separately. In any case, they are fixed, which is very interesting! However, starting from 257, each value will have its applied memory address, which means it will be different every time.


Exception

By the way, if you use the method "assign a value to b", even if the values of a and b both exceed 256, the memory address will be the same. Let's look at a simple example:

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



Therefore, you must be careful when writing programs, especially judgments. It is easy to make mistakes if you are not careful.

In addition, all the above mentioned are the case of writing a program during the interactive interface. In the development environment, the code is often written directly, and then executed by the interpreter after it is written. In this case, the so-called "basic data type" will be cached during execution. Optimization, this will cause the same memory address to be assigned to the variable with the same value during initialization.

For example, I open a new file in the terminal:

vim test.py


Then write in the opened test.py:

# coding: utf-8


# Init
a = 1000
b = 1000


# Id
print('a id:', id(a))
print('b id:', id(b))


# == & is
print('==:', a == b)
print('is:', a is b)



Output:

a id: 140547554998224
b id: 140547554998224
==: True
is: True

Generally speaking, the misunderstandings that == and is are easy to encounter are these. I hope I will not step on such pits again.


References


Read More

Tags:

Leave a Reply