' = = ' compares the values of two objects
' Is ' compares the memory address (ID) of two objects
Below we focus on understanding ' is '. For this, we need to know: Small integer object pool, large integer object pool, and intern mechanism
Small integer pool: Python pre-Creates a small integer cache pool--[-5~256], regardless of how many objects are created, point to the same address, in order to avoid small integers frequently requesting and freeing memory.
>>> a=3>>> b=3 is btrue1+2is3 True>>> a=1.0>>> b=1.0 is bfalse
Large integer Object pool: Python provides an extensible memory space, also known as a universal integer object pool, who needs to use it, eliminating the need for memory. This space is a pyintblock structure that connects a string of memory (block) with a one-way list, which is maintained by Block_list, and each block maintains an array of integer objects (Objects) for storing the cached integer object. That is, a large integer that is in one memory (block) is the same object.
>>> def func (): ... A=10.1... b=10.1 ... return is b ..... >>> func () True
Assignments of A and B are obtained by the same universal integer pool entry.
Intern mechanism: There is a interned in the string type of Python, which is a dictionary of the string objects that are used to ensure that these strings are unique in memory, and that strings of the same value use the same object.
However, only strings made of letters, numbers, and underscores are processed intern, and strings with other characters do not.
>>> a='ABC' >>> b='abc' is Btrue>>> a='ab c'>>> b='ab c' is Bfalse
In addition, Python's other data types such as dictionaries (dict), lists, collections (set), etc., create different objects
>>> a=[1,2,3]>>> b=[1,2,3] is bfalse
>>> a={' a ': 1}
>>> b={' a ': 1}
>>> A is B
False
>>>
To add one, the following is the case because the previous [4,5,6] was recycled and the cache was used in the new [
>>> ID ([1,2,3]) = = ID ([4,5,6]) True> >> ID ([1,2,3])36200264L>>> ID ([4, 5,6])36200264L
Fully understand python's ' = = ' and ' is '