Jun 01, 2021 Article blog
You may often see this problem online and in your study work, but there are still a lot of
python
beginners who don't understand the problem or why it exposes themselves as "rookies."
Here's the question:
Both """ and "is" are operators in Python.
F
or beginners, they may interpret "a"b" as "a equals b" and "a is b" as "a is b".
This may be why beginners confuse "" and "is" in
Python
Before I begin, I'd like to show some examples of using the words
==
and
is
>>> a = 5
>>> b = 5
>>> a == b
True
>>> a is b
True
It's easy, isn't it?
a == b
and
a is b
return the result
True
Let's look at another example:
>>> a = 1000
>>> b = 1000
>>> a == b
True
>>> a is b
False
WTF ?!? T
he only change from the first example to the second example is that the values of a and b range from 5 to 1000. H
owever, the results returned are already different in the "
==
" and
is
.
Let's look at another example:
>>> a = []
>>> b = []
>>> a == b
True
>>> a is b
False
Here's the last example, do you feel like your brain is going to explode?
>>> a = 1000
>>> b = 1000
>>> a == b
True
>>> a is b
False
>>> a = b
>>> a == b
True
>>> a is b
True
The official operation of
==
is
Y
ou typically use the word
==
T
he
a == b
should be interpreted as
a的值是否等于b的值
I
n all of the above examples, the value of a is always equal to the value of b, even for the empty list example.
Therefore,
a == b
is always true.
(Recommended tutorial: python tutorial)
Before I can explain my identity, I need to introduce
id
function. W
e can use
id
function to get the identity of the object. T
his identity is unique and constant for this object throughout the time. T
his identity is unique and constant for the object throughout the time. Y
ou can think of it as the address of this object.
If two objects have the same identity, their values must also be the same.
>>> id(a)
2047616
The operator
is
is to compare whether two objects have the same identity.
a is b
"means "the identity of a is
a的身份与b的身份相同
If you know the actual meaning of
==
and
is
we can start looking at a few of the examples above.
The first is that the results in the first and second examples are different. T
he reason for the different results is that
Python
stores an array of integers between -5 and 256, each with a fixed identity. W
hen we assign an integer variable within this range,
Python
assigns the identification of this variable as an integer in the array list.
As a result, for the first example, because the identities
a
and
b
are obtained from the array list, their identities are of course the same, so
a is b
is
True
>>> a = 5
>>> id(a)
1450375152
>>> b = 5
>>> id(b)
1450375152
However, once the value of the variable is out of range, because there are no objects inside
Python
that have that value,
Python
creates a new identity for the variable and assigns the value to the variable. A
s mentioned earlier, identities are unique to each creation, so even if the values of the two variables are the same, their identities are never equal.
This is why
a is b
turns out to be
False
in the second example.
>>> a = 1000
>>> id(a)
12728608
>>> b = 1000
>>> id(b)
13620208
PS: If you open two consoles and the value is still in that range, you'll get the same identity. However, if the value is not in that range, the result changes.
If you understand the difference between the first and second examples, it is easy to understand the results of the third example. B
ecause
Python
does not store empty list objects,
Python
creates a new object and assigns an Empty List value.
The results are the same whether the two lists are empty or the elements are the same.
>>> a = [1,10,100,1000]
>>> b = [1,10,100,1000]
>>> a == b
True
>>> a is b
False
>>> id(a)
12578024
>>> id(b)
12578056
Next, let's move on to the last example. T
he only difference between the second and last examples is that there is another line of
a = b
which changes the fate of variable a.
The following results will tell you the real reason:
>>> a = 1000
>>> b = 2000
>>> id(a)
2047616
>>> id(b)
5034992
>>> a = b
>>> id(a)
5034992
>>> id(b)
5034992
>>> a
2000
>>> b
2000
As shown above, the identity of a changes to b after
a = b
b.
a = b
assigns the identity of b to a .
Therefore, a and b have the same identity, so the value of a is now the same as the value of b (i.e. 2000).
(Recommended micro-course: python3 basic micro-course)
The last example tells us an important message that we might inadvertently change the value of an object without prior notice, especially if the object is a list.
>>> a = [1,2,3]
>>> id(a)
5237992
>>> b = a
>>> id(b)
5237992
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]
In the example above, because a and b have the same identity, their values must be the same. T
herefore, after adding a new element to a, the value of b is also affected. T
o avoid this, if you want to copy a value from one object to another without referencing the same identity, one of the methods is to use
deepcopy
in the
copy
module.
For lists, we can also do this by
b = a [:]
>>> import copy
>>> a = [1,2,3]
>>> b= copy.deepcopy(a)
>>> id(a)
39785256
>>> id(b)
5237992
Copy
[:]
an element to a new variable using .
>>> a = [1,2,3]
>>> id(a)
39785256
>>> b = a[:]
>>> id(b)
23850216
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3]
What's the difference between " and "is"? R elated content, after reading these is not feel fully understand the difference between the two. Don't get me wrong when you encounter this kind of problem in the interview later.