Full stack engineer a detailed explanation of Python and the interview is the difference between ==! After reading really learned!

Intern interview when asked when the difference is and ==, many students could not answer, when both return consistent confused, and when returned inconsistent. In this article we look at the difference between the two.

Let's look at a few examples:

a = "hello"
b = "hello"
print(a is b)  # 输出 True 
print(a == b)  # 输出 True a = "hello world" b = "hello world" print(a is b) # 输出 False print(a == b) # 输出 True a = [1, 2, 3] b = [1, 2, 3] print(a is b) # 输出 False print(a == b) # 输出 True a = [1, 2, 3] b = a print(a is b) # 输出 True print(a == b) # 输出 True 

Why is some of the same results and output the result == above, some do not do the same? Let's look at the official documentation and interpretation is == under.

The official said the document is represented object identifier (object identity), and is represented by an equal == (equality). It is used to check whether the role is the object identifier is consistent, that is to compare two objects in memory address whether the same, and is used to check whether == two objects are equal.

We checked a is b, when in fact the equivalent of checking id (a) == id (b ). To inspect a == b, when the object is actually a call of  EQ ()  method, a == b corresponds a.eq (b).

In general, if a return True if IS b, i.e. a and b point to the same memory address, then return True also a == b, i.e. a and b are equal.

After Well, understand the above explanation, we look at a few examples of previous

a = "hello"
b = "hello"
print(id(a))   # 输出 140506224367496
print(id(b))   # 输出 140506224367496
print(a is b) # 输出 True print(a == b) # 输出 True a = "hello world" b = "hello world" print(id(a)) # 输出 140506208811952 print(id(b)) # 输出 140506208812208 print(a is b) # 输出 False print(a == b) # 输出 True a = [1, 2, 3] b = [1, 2, 3] print(id(a)) # 输出 140506224299464 print(id(b)) # 输出 140506224309576 print(a is b) # 输出 False print(a == b) # 输出 True a = [1, 2, 3] b = a print(id(a)) # 输出 140506224305672 print(id(b)) # 输出 140506224305672 print(a is b) # 输出 True print(a == b) # 输出 True 

Printing a rear id (a) and id (b) very clear. As long as values ​​of a and b are equal, a == b will return True, and have only ID (a) and ID (b) are equal, a is b before returning True.

There is also the question of why a and b are both "hello" when, a is b returns True, and a and b are the "hello world" time, a is b it returns False?

This is because in the former case Python string resides mechanism played a role. For smaller strings, in order to improve system performance Python will retain a copy of its value when creating a new string when you can point directly to the copy. So "hello" is only one copy of the same in memory of a and b id value, and "hello world" is a long string, not memory-resident, Python respective object is created to represent a and b, so their value identical but different id values. (This interpretation is wrong, very grateful to the students correct bubble bubbling students pointed out: nothing to do intern mechanism and the length of the string, in interactive mode, string literals per line will apply a new string, but only with case letters, numbers and underscores will be intern, that is, maintains a dict to make these strings globally unique)

To sum up, it is to check whether two objects point to the same memory space, while == is to check their values ​​are equal. As can be seen, is a more stringent checks than ==, is the return of True indicates that these two objects point to the same memory, the same value is also certain. In the process of learning Python, often because there is no information or guidance resulting in no one do not want to learn anymore, so I had prepared a group 592,539,176, the group has a large number of PDF books, tutorials gave us free! No matter what stage of learning to small partners can get to the information they corresponding!

See here, we are not and they get to know the difference between == is it?

Then we go one step further and think about the following questions:

Python, and None comparison, why is None rather than == None of it?

Welcome to answer in the comments area ~

Guess you like

Origin www.cnblogs.com/chengxyuan/p/11919270.html