In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of the traps in Python, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading the traps in this Python. Let's take a look at it.
In the following question, can you see the nature of the problem at a glance?
# Group 1 > a = 256 > b = 256 > an is b
True
# Group 2 > a = 257 > b = 257 > an is bFalse
# Group 3 > a = 257; b = 257 > > an is bTrue
Whether in a Python2 or Python3 environment, as long as you execute it in CPython's interactive command line REPL, the result is no different.
We know that is compares whether the memory addresses of two objects are the same (the id function returns a value related to the memory address of the object). The implication is to see whether the two variables point to the same object. Let's look at the id value of each variable.
> a = 256
> > id (a)
1721788128
> b = 256
> > id (a)
1721788128
> a = 257
> > id (a)
14947024
> b = 257
> > id (b)
14947104
> a = 257; baked 257
> > id (a)
14947136
> > id (b)
14947136
> > >
As expected, the id values of the two groups are the same, only the middle group of id values are different, we can briefly analyze the reasons. In Python, everything is an object, and theoretically any two objects have different id values, for example:
>
> id (nums)
15148936
> nums2 = [1, 2, 2, 3]
> id (nums2)
15160824
> nums3 = [1, 2, 2, 3]
> id (nums3)
15160864
You can see that the id value of each object is different, and even if the values (contents) of two objects are the same, their id values are different (nums2 and nums3).
So why is the id value of the first group of two objects the same? Maybe some students already know.
Because in Python, Python will create objects for us when we need them, and recycle them when they are not needed, just like cleaning things in the house in time after they are used up, otherwise the whole room will soon be full, resulting in no more stuff in the room.
Similarly, in order to improve performance, Python caches some commonly used integers, just like there are some things in the house that have to be used frequently every day, such as a bed. You can't move the bed out after sleeping, use it and then move it back. This is too inefficient, because the handling process is so time-consuming. Therefore, we can take a special space to put the bed.
The same is true in Python, because integers are objects we often use. In order to avoid repeated creation and recycling, just cache those commonly used integers and take them directly from the cache every time you need to use them instead of recreating them (it must be a brand new object if you re-create them). The range of these integers is [- 5256]. Of course, this range of numbers is determined by the father of Python, and you must recompile the Python environment if you want to change it.
Now we can explain why the first group is True and the second group is False.
Why is the third group result True again? Isn't it said that integers greater than 256 are no longer cached and are new objects every time they are used? Don't worry, listen to me again.
Or for performance reasons, Python has been further optimized internally, how to optimize it? But for all code in the same block, if two integers with the same value appear, they will be reused, as shown in the following code:
# test.py
#-*-coding: utf-8-*-
A = 257
B = 257
Def func ():
C = 257
Print (an is c) # False
Print (an is b) # True
Func ()
The above code is in a test.py file. At runtime, the id values of an and b are the same, but the id values of c are different from a, because an and b are in the same code block and belong to the module level, while c is in the function, belongs to local variables, and they do not belong to the same code block, so the 257 object in the function will be recreated. When creating b, it is found that there is a 257 value in the same code block. The object is reused.
Going back to the third set of values mentioned earlier, in Python's interactive command line REPL, each individual line is treated as a code block, and the code on the same line belongs to the same code block, so it is not difficult to understand that an and b in the third group are in the same code block, so the latter reuses the former, so the id of the two variables is the same.
Do you think this is a pit. Although we may not be able to use it in our actual scenario, at least we know some of the optimizations that Python has done for us.
This is the end of the article on "what are the traps in Python?" Thank you for reading! I believe you all have a certain understanding of the knowledge of "what are the traps in Python". If you want to learn more, 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: 270
*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.