Well, I think that the fact that they are not the same is fairly pervasive, but Then __ I would like to explain what the hell are you comparing __.
It seems that there are always mistakes in the article, so I'd be happy if you could point it out (I'd be happy if there was feedback)
I will explain it briefly (if you understand it, you can skip it) Look at Example 1 first
Example 1
a = "Rabbit house"
b = "Rabbit house" #Same string as a
c = "Kantoan" #Character string different from a
#a and c==Compare with
print(a == c) # False
#Compare a and c with is
print(a is c) # False
#a and b==Compare with
print(a == b) # True
#Compare a and b with is
print(a is b) # False
Rabbit House
and Kantoan
are naturally different, so ʻa == c and ʻa is c
are both False
.
On the other hand, ʻaand
blook the same at first glance ...
True when ʻa == b
False
when ʻa is b`
The result is different
Well the main subject
I will explain based on Example 2.
Example 2
a = "Rabbit house"
b = "Rabbit house" #Same string as a
# ==Compare with
print(a == b) # True
#Compare with is
print(a is b) # False
print(id(a)) #object id of a
print(id(b)) #object id of b(Returns a value that is not the same as a)
In Python, all data is an object. ʻId () `is a function that returns the id of the argument object (hereinafter referred to as the object id).
==
returned True
because ʻa and
b were __the same string __. However, ʻa is b
returned False
, even though it was the same string.
This is because ʻis` is a __ operator that compares __ object ids.
Checks if ==
is __equivalent __.
On the other hand, ʻis` is checking if it is __ident __.
The difference is here
--The object id returned by the id () function becomes the address of the memory in which the object resides when multiplied by hex (). --There are exception cases (see below)
It is enough to understand the above contents, but there are exception cases, so I will introduce it for the time being.
Example 3
a = 100
b = 100
print(a == b) #Is it equivalent
print(a is b) #Is it the same
Will it be True or False?
Those who understand so far are probably
You would expect ʻa == b to return
True and ʻa is b
to return False
.
But __ both are True
... __
*** These comparison operators should be generalized, but there are exceptions and they are crazy. *** ***
I would think. I thought so at first (´ ・ ω ・ `)
However, there is a good reason for this. From Plain Integer Objects — Python 2.7.13 documentation --PyObject * PyInt_FromLong
The current implementation keeps an array of integer objects for all integers between -5 and 256
To explain in Japanese
The current implementation of Python holds an array of integer objects from -5 to 256
There is. So Python always keeps arrays from -5 to 256 in memory.
a = b = range(-6, 258) #-Generate an array from 6 to 257
for x,y zip(a, b):
print(x, x is y) #Compare if two equivalent values are the same
-6 False
-5 True
-4 True
.
.
(Omission)
.
.
255 True
256 True
257 False
From -5 to 256, you returned True
because the object ids are the same. On the other hand, it returns False
for -6 and 257 (first and last values) that are out of the range of values held by memory.
It's confusing, but you should remember this too.
Other
The is comparison between True
s and between False
s always returns True
. This is because there is always only one True
and False
in memory.
There is another exception as far as I know, but I will omit it here.
If you have any suggestions, please leave them in the comments section!
Recommended Posts