The title says it all – this PEP proposes a newprint() builtinthat replaces theprint statement and suggests a specific signaturefor the new function.
Theprint statement has long appeared on lists of dubious languagefeatures that are to be removed in Python 3000, such as Guido’s “PythonRegrets” presentation[1]. As such, the objective of this PEP is notnew, though it might become much disputed among Python developers.
The following arguments for aprint() function are distilled from apython-3000 message by Guido himself[2]:
print is the only application-level functionality that has astatement dedicated to it. Within Python’s world, syntax is generallyused as a last resort, when somethingcan’t be done without help fromthe compiler. Print doesn’t qualify for such an exception.print output by something more sophisticated, likelogging calls or calls into some other I/O library. With aprint()function, this is a straightforward string replacement, today it isa mess adding all those parentheses and possibly converting>>streamstyle syntax.print puts up a much larger barrier forevolution, e.g. a hypothetical newprintf() function is not toofar fetched when it will coexist with aprint() function.print statements into another callif one needs a different separator, not spaces, or none at all.Also, there’s no easy wayat all to conveniently print objects withsome other separator than a space.print() is a function, it would be much easier to replace it withinone module (justdefprint(*args):...) or even throughout a program(e.g. by putting a different function in__builtin__.print). As it is,one can do this by writing a class with awrite() method andassigning that tosys.stdout – that’s not bad, but definitely a muchlarger conceptual leap, and it works at a different level than print.The signature forprint(), taken from various mailings and recentlyposted on the python-3000 list[3] is:
defprint(*args,sep=' ',end='\n',file=None)
A call like:
print(a,b,c,file=sys.stderr)
will be equivalent to today’s:
print>>sys.stderr,a,b,c
while the optionalsep andend arguments specify what is printedbetween and after the arguments, respectively.
Thesoftspace feature (a semi-secret attribute on files currentlyused to tell print whether to insert a space before the first item)will be removed. Therefore, there will not be a direct translation fortoday’s:
print"a",print
which will not print a space between the"a" and the newline.
The changes proposed in this PEP will render most of today’sprintstatements invalid. Only those which incidentally feature parenthesesaround all of their arguments will continue to be valid Python syntaxin version 3.0, and of those, only the ones printing a singleparenthesized value will continue to do the same thing. For example,in 2.x:
>>>print("Hello")Hello>>>print("Hello","world")('Hello', 'world')
whereas in 3.0:
>>>print("Hello")Hello>>>print("Hello","world")Hello world
Luckily, as it is a statement in Python 2,print can be detectedand replaced reliably and non-ambiguously by an automated tool, sothere should be no major porting problems (provided someone writes thementioned tool).
The proposed changes were implemented in the Python 3000 branch in theSubversion revisions 53685 to 53704. Most of the legacy code in thelibrary has been converted too, but it is an ongoing effort to catchevery print statement that may be left in the distribution.
This document has been placed in the public domain.
Source:https://github.com/python/peps/blob/main/peps/pep-3105.rst
Last modified:2025-02-01 08:59:27 GMT