In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the difference between "is" and "=" in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
In Python, you can use "is" and "=" to compare whether two objects (variables) are equal, but what's the difference between them? When to use "is" and when to use "="? During the interview, I found it difficult for many candidates to explain the two completely, so in this article, "the Zen of Python" will make a deep and simple comparison between the two.
Let me give you an example.
Recently, Xiao Huang is so rich that he spent a lot of money on a P90D Tesla. Let's call this car "Xiao P" for the time being. This car is exactly the same as the car of the old Wang family next door (the car is called "Xiao Wang"). No matter it is the same model, appearance or price, it is produced in the same batch. Here we can say that "Xiao P" and "Xiao Wang" are exactly the same and equal (euqal), but in essence they are two different objects. One day Xiaojun gave his car another screen name "Aiju". When we say "Xiao P", we are actually talking about "Aiju", because in essence, the two names refer to the same object. Here, we call "Xiao P" and "Aiju" exactly equal (identical).
In Python, the difference between "= =" and "is" can be compared to this example. The former is an equality comparison, which compares whether the values in two objects are equal, and the latter is a consistency comparison, which compares whether the memory space addresses of the two objects are the same.
Obviously, if the memory address is the same, then their values must be the same, so if "is" returns True, then "=" must also return True, and vice versa.
Talk is cheap, show me the code
First create a list object, then give it a name a, and then define another variable b to point to the same object.
> a = [1,2,3]
> b = a
The printed values of an and b are equal because the two variables point to the same object, just like giving a car two different names.
> > a
[1, 2, 3]
> > b
[1, 2, 3]
Of course, both is and = = return True.
> a = = b
True
> an is b
True
To create a new object, although the value is the same, they are essentially two different objects in two different memory spaces, so "is" returns False, but their values are the same, so the "=" operation returns True.
> c = [1, 2, 2, 3]
> an is c
False
> a = = c
True
"is" returns True only when the two variables compared point to the same object, and "= =" ultimately depends on the object's _ _ eq__ () method. In essence, the object's _ _ eq__ () method is called when the two variables are compared. For example:
> > class Foo (object):
Def _ _ eq__ (self, other):
Return True
> f = Foo ()
> > f = = 1
True
> f = = None
True
> f is None
False
Because the eq method of the custom class Foo always returns True, it returns True when "= =" with any object. It and None are two different objects, so the 'is' operation returns False.
Finally, please think about this code, why the same operation will have different results.
> a = 257
> b = 257
> an is b
False
> a = 123
> b = 123
> an is b
True
What's the difference between "is" and "=" in Python shared by the editor? If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.