Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the hidden features of Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the hidden features of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what hidden features Python has.

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 a string will be hosted:

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'). The relevant source code is implemented in [address 3].

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.)

But in the latest version 3.8, the result has changed back. Although version 3.8 is the same as version 3.7, it uses the AST optimizer. It is not clear what official adjustments have been made to version 3.8 of AST.)

Thank you for your reading, the above is the content of "what hidden features Python has". After the study of this article, I believe you have a deeper understanding of what hidden features Python has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report