In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Python string resident and constant folding hidden features are how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Here are some subtle features of the Python string that will definitely surprise you.
Case 1:
Case 2:
Case 3:
It's easy to understand, right?
Description:
These behaviors are due to the fact that when Cpython compiles and optimizes, in some cases it tries to use an immutable object that already exists instead of creating a new object every time. (this behavior is called string resident [string interning]).
After a resident occurs, many variables may point to the same string object in memory. (thus saving memory)
In the above code, the string is implicitly resident. When implicit residency occurs depends on the specific implementation. Here are some ways to guess whether the string will be resided
All strings of length 0 and length 1 are resided.
Strings are implemented at compile time ('wtf' will reside, but' .join (['wicked,' tweak,'f']) will not be resided)
The string will reside when it contains only letters, numbers or underscores. So 'wtfenced' Because of the inclusion! Without being stationed. CPython's implementation of this rule can be found in address 1.
When the values of an and b are set to "wtf!" on the same line The Python interpreter creates a new object and references the second variable at the same time. If you assign values on different lines, it won't "know" that there is already a wtf! Object (because of "wtf!" It is not implicitly stationed in the way mentioned above. It is a compiler optimization and is especially suitable for interactive environments.
Constant folding (constant folding) is a peephole optimization (peephole optimization) technique in Python. This means that the expression'a clock 20 'will be replaced with' aaaaaaaaaaaaaaaaaaaa' at compile time to reduce the clock cycle at run time. Constant folding occurs only for strings less than 20 in length. Why? Imagine the size of the .pyc file generated as a result of the expression'a hundred percent 10').
If you run the above sample code in version 3.7, you will find that some of the code runs the same as the comments. This is because in version 3.7, constant folding has moved from the peephole optimizer to the new AST optimizer, which performs optimizations with greater consistency. (contributed by Eugene Toder and INADA Naoki in bpo-29469 and bpo-11549.)
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.