Detailed Explanation of is and = = and String Residence Mechanism in Python

  • 2021-07-03 00:41:14
  • OfStack

is and = =

Let's take a look at the concepts of is and = = in the official documents. is denotes the object identifier (object identity), while = = denotes equality (equality); The function of is is to check whether the identifier of an object is 1, that is, to compare whether the addresses of two objects in memory are 1 (equivalent to checking id (a) = = id (b)), while = = is to check whether the values referenced by two objects are equal (equivalent to checking a. eq (b)); This is somewhat similar to Java, except that in Java, = = is used to compare the addresses of two objects in memory, and equals () is used to check whether the values between them are equal.

is ==
概念 对象标示符 相等
作用 比较对象在内存中的地址 检查两个对象引用的值
示例 id(a) == id(b) a.eq(b)

String resident mechanism

Strings in Python adopt intern mechanism. When strings with the same value are needed (such as identifiers), they can be used directly from the string pool to avoid frequent creation and destruction, improve efficiency and save memory. Therefore, splicing and modifying strings will affect performance.

Because it is immutable, the operation of strings is not replace, but a new object, which is why it is not recommended to use + but join () when splicing multiple strings. join () calculates the length of all strings first, and then copies them, only new1 objects.

It should be noted that not all strings will adopt intern mechanism, and only strings containing only underscores, numbers and letters will be intern.

Related examples

Example 1


a = "hello"
b = "hello"
print(a is b) #  Output  True 
print(a == b) #  Output  True

A simple string object with the same value will only be saved in one copy in the string pool, which determines that the string must be an immutable object, so a and b are the same object

Example 2


a = "hello world"
b = "hello world"
print(a is b) #  Output  False
print(a == b) #  Output  True 

Both a and b have spaces, so they will not be intern (spaces are not python identifiers), so a and b are not the same object. Note that this is only performed on the interactive command line, but in PyCharm or save as a file, the result is different, mainly because the interpreter has been optimized in part

Example 3


a = 'ab' + 'c' is 'abc'
print(a) #  Output  True
ab = 'ab'
b = ab + 'c' is 'abc'
print(b) #  Output  False

The first 'ab' + 'c' is evaluated at compile-time (compile time) and is replaced by 'abc', so the output is True; In the second example, ab + 'c' is spliced at run-time (runtime), resulting in not being automatically intern

Example 4


a = [1, 2, 3]
b = [1, 2, 3]
print(a is b) #  Output  False
print(a == b) #  Output  True 

a and b are lists, not the same object

Example 5


a = [1, 2, 3]
b = a
print(a is b) #  Output  True 
print(a == b) #  Output  True

Copy the reference of a to b (reference assignment), which actually points to the same object in memory

Example 6


a = ["I", "love", "Python"]
b = a[:]
print(a is b) #  Output  False
print(a == b) #  Output  True
print(a[0] is b[0]) #  Output  True
print(a[0] == b[0]) #  Output  True

b reassigns objects (slicing assignments) through slicing operations, but the values are the same as a. Because the slice copy is a shallow copy, which means that the elements in the list have not been recreated, a [0] is b [0] output is True

Example 7


a = 1
b = 1
print(a is b) #  Output  True
print(a == b) #  Output  True

Python caches smaller integer objects and fetches them directly from the cache the next time you use them

Example 8


a = 320
b = 320
print(a is b) #  Output  False
print(a == b) #  Output  True

Python caches only smaller integer objects (range [-5,256]), not all integer objects. Note that this is only performed on the interactive command line, and the result is different when executed on PyCharm or save as a file, mainly because the interpreter has been optimized in part

Comparison of is with = =

is is faster than = = because it cannot be overloaded without special function calls. By comparing two integers id directly, the overhead of function calls is reduced. While a = = b is equivalent to a. eq (b). The eq method inherited from object originally compared id of two objects, and the result is similar to is 1. However, most Python objects override eq methods that override object, and define relevant comparisons of contents, so the values of object attributes are compared.

is should be used when comparing variables with singleton values. At present, the most common place to use is is when judging whether the object is None. The following is the recommended writing: xxx is None;; The recommended writing for judging not to be None is: xxx is not None


Related articles: