♚ \

Pea flower under the cat, a 985 college graduates, both geek thinking and humanistic feelings. Focus on Python technology, data science and deep learning to create a fun and useful learning sharing platform.

\

Hello, I’m Cat Brother. Today’s post is a translation of PEP, the translation project has been suspended for some time, I am now on a whim to renew it.

This PEP is about changing print to a function in Python 3 and was published in 2006. I learned Python using 3, as I’m sure most readers do, but this article isn’t completely out of date.

On the one hand, there are many Python 2 users who need to understand this (although it’s not difficult), and on the other hand, more importantly, this PEP documents a process of evolution that allows you to understand the ins and outs of a function, the details of a design, and the considerations behind it. Each PEP is decided upon by several core developers after many discussions, so it is a condensed product, and reading it may give us some benefit. This is also my original intention to translate.

Copy the code

PEP 3105– Change print to a function

The original PEP: www.python.org/dev/peps/pe…

PEP title: Make print a function

PEP作者: Georg Brandl

Date created: 2006-11-19

Integrated version: 3.0

Translator: Peas flower under the cat

Translation project: github.com/chinesehuaz…

Abstract

The title says it all — this PEP proposes to replace the print statement with a new built-in function print() and to give this new function a special signature.

The principle of this

The print statement has long been on the list of unreliable language features, such as Guido’s Python Regrets talk [1], and is scheduled to be removed in the Python 3000 release. Thus, the purpose of this PEP is not new, although it may be more controversial among Python developers.

The following argument about the print() function is taken from Guido’s own Python-3000 message [2] :

Design specifications

The way print() is written is taken from various mails, and the most recent published in the Python-3000 list is [3] :

def print(*args, sep=' ', end='', file=None)
Copy the code

Call like:

print(a, b, c, file=sys.stderr)stderr)
Copy the code

Equivalent to current:

print >>sys.stderr, a, b, c
Copy the code

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

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

print "a",print"a",
print
Copy the code

It does not print a space between “a” and a newline character.

(In version 3.3, the print() function was changed to add default flush=False.)

Backward compatibility

The changes proposed in this PEP will invalidate today’s print statement. Only those that happen to enclose all arguments in parentheses will be valid in Python 3, and for the rest, only parenthesized values will be printed as they are. For example, in 2.x:

>>> print ("Hello")Hello>>> print ("Hello", "world")('Hello', 'world')"Hello")
Hello
>>> print ("Hello", "world")
('Hello', 'world')
Copy the code

In 3.0:

>>> print ("Hello")Hello>>> print ("Hello", "world")Hello world"Hello")
Hello
>>> print ("Hello", "world")
Hello world
Copy the code

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

implementation

The changes will be implemented in the Python 3000 branch (revision from 53685 to 53704). Most of the legacy code has already been converted, but it will take constant effort to catch every print statement in the distribution.

The resources

[1] legacy.python.org/doc/essays/…

[2]Python 3.0 replace print (Guido van Rossum)

Mail.python.org/pipermail/p…

[3] Guido van Rossum: print() in py3K

Mail.python.org/pipermail/p…

copyright

This document has been placed in the public domain. The source document:

Github.com/python/peps…