In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the Python integer problem". 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 how to understand the Python integer problem.
In Python, everything is an object, and without exception integers are also objects. Whether the objects are equal or not can be compared with = = or is. The difference between the = = and is operation is:
Is compares whether the id values of two objects are equal, that is, whether they are the same instance object and point to the same memory address.
= = comparing whether the contents of two objects are equal, the _ _ eq__ () method of the object is called by default.
After knowing the difference between is and = =, you may encounter the following confusion. Let's take a look at two pieces of code:
Clip 1:
> a = 256 > b = 256 > a = = b True >
Segment 2:
> a = 256 > > b = 256 > an is b True >
Execute the above two pieces of code on the interactive command line, and it's easy to understand that aura _ roomb in snippet 1 returns True, because both objects have a value of 256. for fragment 2, an is b also returns True, which means that an and b point to the same object, so you can check whether their id values are equal:
> > id (a) 8213296 > > id (b) 8213296 >
It turns out that they are indeed the same object and point to the same memory address. Is it true that all integer objects are the same instance object as long as the values (contents) of two objects are equal? In other words, will the operation return True,is as long as = = return True for integer objects? With this question, take a look at the following two pieces of code:
Clip 1:
> a = 257 > b = 257 > a = = b True >
Segment 2:
> a = 257 > > b = 257 > an is b False >
For 257 an is b returned False, the result may be unexpected or unexpected, but in any case, we still have to get to the bottom of the matter and find out the truth of the problem.
Puzzle one
For performance considerations, Python has done a lot of internal optimization work. For integer objects, Python caches some frequently used integer objects and saves them to a linked list called small_ints. In the entire life cycle of Python, any place that needs to reference these integer objects will not re-create new objects, but directly reference objects in the cache. Python places these potentially frequently used integer objects in the small_ints for small objects in the range of [- 5256], but whenever you need to use small integers, you take them from there instead of temporarily creating new objects. Because 257 is no longer in the range of small integers, although the values of an and b are the same, they exist as two separate objects within the Python, going their own way and not interfering with each other.
After figuring out the * * questions, let's go on to write a function on the Python interactive command line, and take a look at the following code:
Clip 1:
> c = 257 > def foo ():... A = 257. B = 257. Print an is b... Print an is c... > > foo () True False
Er, what is the situation? yes, you read it correctly. When the code an and b values in fragment 1 are all 257, an is b returns True, while an is c returns False,a, b, and c values of 257. Why are the results different? Is this perception that has just been established completely rejected, so what happened in this code? Is it possible that the conclusion of Puzzle No. 1 is wrong?
Puzzle two
In order to understand this problem, it is necessary to understand the concept of program code block. The Python program consists of code blocks, which are executed as the smallest basic unit of the program. A module file, a function body, a class, and a single line of code in an interactive command are all called a code block. In the above code, it consists of two code blocks, c = 257 as one code block and the function foo as another code block. In order to further improve the performance of Python, if an integer object created in a code block has an object with the same value in the code block, it will be referenced directly, otherwise a new object will be created. Python for performance considerations, but all immutable objects, objects in the same code block, only objects with the same value will not be created repeatedly, but directly reference existing objects. Therefore, not only integer objects but also string objects follow the same principle. So an is b naturally returns True, while c and an are not in the same code block, so two objects with values of 257 are created inside Python. To verify the conclusion, we can use the dis module to look at this code from a bytecode perspective.
> import dis > dis.dis (foo) 2 0 LOAD_CONST 1 (257) 3 STORE_FAST 0 (a) 3 6 LOAD_CONST 1 (257) 9 STORE_FAST 1 (b) 4 12 LOAD_FAST 0 (a) 15 LOAD_FAST 1 (b) 18 COMPARE_OP 8 (is) 21 PRINT_ITEM 22 PRINT_NEWLINE 5 23 LOAD_FAST 0 (a) 26 LOAD_GLOBAL 0 (c) 29 COMPARE_OP 8 (is) 32 PRINT_ITEM 33 PRINT_NEWLINE 34 LOAD_CONST 0 (None) 37 RETURN_VALUE
You can see that both 257s are obtained from the same location in the constant pool co_consts [1].
At this point, I believe that everyone on the "Python integer problem how to understand" have a deeper understanding, might as well to the actual operation of it! 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.