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--
The main content of this article is to explain "what is the difference between python and is". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between python and is?"
The understanding of these two operators can largely reflect a person's proficiency in python. Their differences can be summarized as the following two points.
= = is the comparison operator, and is is the identity operator
= = compare whether the values of two objects are equal. Is compares whether the memory addresses of two objects are the same.
In practical application, the use of these two operators is often puzzling.
A = [1]
B = [1]
Print (a = = b)
Print (an is b)
The result of the program output
True
False
There should be no objection to the result of the expression a = = b. Differences and puzzles appear in the second expression, an is b, where the is identity operator compares the memory addresses of two objects. Obviously, the memory addresses of the lists pointed to by variables an and b are different.
When you use the assignment statement, the interpreter creates a list [1] in memory, which occurs twice. Their memory addresses are different, and because an and b are two different lists, they can store data separately without affecting each other, so the key to understanding this problem is that when the line of code a = [1] is executed, a new list [1] is created in memory.
Memory pool
Curious people may immediately write code experiments to verify what has been said above.
A = [1]
B = [1]
Print (id (a), id (b))
Program output result
4714131208 4714127880
The built-in function id () can return the memory address of the object, and they are different, but there is always something wrong, and the following code cannot be explained by the previous theory.
A = 1
B = 1
Print (a = = b)
Print (an is b)
Program output result
True
True
As mentioned earlier, if you execute the assignment statement twice, you should create two 1s in memory, their memory addresses should be different, the result of an is b should be False, but the actual running code will get True. Why?
Unlike lists, int type data is immutable, and two variables pointing to an int type data at the same time will not have unexpected consequences, so python uses memory pool technology. Python designers believe that integers in the range of-5 to 256 are often used and should not be created and destroyed frequently, so a memory pool is established, and there is only one number between-5 and 256. use them at any time. The one in the memory pool is used and will not be recreated, so they have the same memory address.
Interactive interpreter
If you are still curious, you may continue to experiment to verify the previous theory.
A = 257
B = 257
Print (a = = b)
Print (an is b)
Program output result
True
True
Once again confused, not agreed-5 to 256 in the memory pool, now using 257, why the result of an is b is also True?
This code, if you want to get the theoretical results, must be executed in the python interactive interpreter, if you are executed in pycharm, or through the python command to execute the script, the result is True, because when running in these ways, it will be optimized during the compilation phase, because there is really no need to create two 257s.
If you run this code in the python interactive interpreter, you can witness the correctness of the theory.
> a = 257
> b = 257
> a = = b
True
> an is b
False
At this point, I believe you have a deeper understanding of "what is the difference between python and is?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.