Difference in python 'IS' and '==' 'is

Difference in python 'IS' and '==' 'is

Everything is an object in Python.

Before talking about is both operator and == difference, we must first know the three basic elements of Python objects included are:id (identity), type (data type) and value (value).

Equality comparison between the object can ==, it can also be used is.

== object and is compared are determined action, but the object is not the same as the contents of the comparison determination, below which look at the specific difference.

  1. == python is the standard operators in comparison operator for comparing two objects of determination value (value) is equal, for example, comparison between the following two strings:
>>> a = 'cheesezh'
>>> b = 'cheesezh'
>>> a == b
True
  1. is also known as the identity operator, the operator determines that the comparison between the object unique identifier, i.e. whether the same id. By comparison among the following list of a few, you will understand the working principle is the identity operator:
>>> x = y = [4,5,6]
>>> z = [4,5,6]
>>> x == y
True
>>> x == z
True
>>> x is y
True
>>> x is z
False
>>>
>>> id(x)
3075326572
>>> id(y)
3075326572
>>> id(z)
3075328140

The first three are True, False why the last one is it? The value of x, y and z are the same, so the first two problems is not True. As for why the last one is False, take a look at three objects are what id will understand.

Next, look at an example, a and b of (a == b) are the same type is True, and (a is b) are not.

>>> a = 1 #a和b为数值类型
>>> b = 1
>>> a is b
True
>>> id(a)
14318944
>>> id(b)
14318944
>>> a = 'cheesezh' #a和b为字符串类型
>>> b = 'cheesezh'
>>> a is b
True
>>> id(a)
42111872
>>> id(b)
42111872
>>> a = (1,2,3) #a和b为元组类型
>>> b = (1,2,3)
>>> a is b
False
>>> id(a)
15001280
>>> id(b)
14790408
>>> a = [1,2,3] #a和b为list类型
>>> b = [1,2,3]
>>> a is b
False
>>> id(a)
42091624
>>> id(b)
42082016
>>> a = {'cheese':1,'zh':2} #a和b为dict类型
>>> b = {'cheese':1,'zh':2}
>>> a is b
False
>>> id(a)
42101616
>>> id(b)
42098736
>>> a = set([1,2,3])#a和b为set类型
>>> b = set([1,2,3])
>>> a is b
False
>>> id(a)
14819976
>>> id(b)
14822256

As can be seen by the above example, the case where only numeric and string, a b before IS is True, when a and b are tuple, list, dict or set type, b is a IS False.

  1. Python would be relatively small integer object cache, next time use directly from the cache, and it is possible the same == results:
>>> a = 1
>>> b = 1
>>> a is b
True
>>> a == b
True

And look at another piece of code:

>>> a = 257
>>> b = 257
>>> a is b
False

This is what causes it?
Note that, Python contrast only small Integer object cache (in the range [-5, 256]) cached, and not all the Integer object. Note that this is just execute the command line, and execute the file or save in Pycharm, the result is not the same, because the interpreter made a part of optimization.

  1. is compared with == have a comparative advantage, that is, fast speed, because it can not be overloaded, without performing a special function call, less the function call overhead and direct comparison of two integers id. And a == b is equal to A. EQ (B). Inherited from the object of EQ the above mentioned id method to compare two objects, and the result is the same. But most Python object overrides __eq__ method object, and compare relevant custom content, so the comparison is the value of the object's properties.
在变量和单例值之间比较时,应该使用 is。
目前,最常使用 is 的地方是判断对象是不是 None。下面是推荐的写法:
a is None
判断不是None的推荐写法是:
a is not None

to sum up:

  1. "Is" id value comparing two objects are equal, a point to the same memory address;
  2. "==" compares the contents of two objects are equal, values ​​are equal;
  3. Small Integer object [-5,256] is in the range of global interpreter is placed in the cache for reuse;
  4. "Is" operator than the "==" high efficiency, when compared None variable and should be used is.
Published 60 original articles · won praise 6 · views 1354

Guess you like

Origin blog.csdn.net/weixin_45775963/article/details/103702282