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 is the principle of Python3 changing print into a function?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is to explain "what is the principle of Python3 changing print into a function". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the principle of Python3 changing print into a function"?

Principle exposition

Print statements have long been listed in the list of unreliable language features.

Print is the only application-level functionality and has proprietary statements. In the world of Python, syntax is often used as a last resort when certain tasks cannot be accomplished without the help of a compiler. In this abnormal situation, print is not appropriate.

When developing applications, people often need to replace print output with something more complex, such as calling logging or calling other Ibino libraries. As for the print () function, this is a straightforward character substitution that now mixes all those parentheses and may convert > > stream-style syntax.

Setting a special syntax for print will only bring a bigger barrier to evolution. For example, there is a guess that a new printf () function will soon appear and coexist with the print () function.

When you need a different delimiter (not a space, or no delimiter), there is no easy way to convert the print statement into another call. Similarly, when you use some other delimiters instead of spaces, it is not easy to print objects at all.

If print () is a function, you can easily replace it within a module (just def print (* args):... ) and can even be replaced throughout the program (for example, put a different method into _ _ builtin__.print). In fact, to do this, you can also write a class with the write () method and direct it to sys.stdout, which is a good idea, but it's certainly a huge conceptual leap, and it works at a different level than print.

Design specification

Print () is written in a variety of emails:

Def print (* args, sep='', end='\ nkeeper, file=None)

Call something like:

Print (a, b, c, file=sys.stderr)

Equivalent to the current:

Print > > sys.stderr, a, b, c

The optional sep and end parameters specify the content between and after each print parameter accordingly.

The softspace function (the current semi-secret attribute on the file that tells print whether to insert a space in the first bar) will be removed. Therefore, the following words in the current version cannot be converted directly:

Print "a", print

It does not print a space between the "a" and the newline character.

(translation note: in version 3. 3, the print () function has been changed to add the default parameter flush=False)

Backward compatibility

The changes proposed in this PEP will invalidate today's print statements. Only those words that happen to enclose all parameters in parentheses will take effect in the Python 3 version. As for the rest, only parenthesized values can be printed as is. For example, in 2.x:

> print ("Hello") Hello > print ("Hello", "world") ('Hello',' world')

In 3.0:

> print ("Hello") Hello > print ("Hello", "world") Hello world

Fortunately, because print is a statement in Python 2, it can be detected by automated tools and replaced reliably and accurately, so there should be no major migration problems (if anyone writes the tool).

Realize

The changes will be implemented in the Python 3000 branch (revisions from 53685 to 53704). Most of the conversion has been done in the legacy code, but it takes constant effort to capture every print statement in the distribution.

At this point, I believe you have a deeper understanding of "what is the principle of Python3 changing print into a function". 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.

Share To

Development

Wechat

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

12
Report