Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 3105 – Make print a function

Author:
Georg Brandl <georg at python.org>
Status:
Final
Type:
Standards Track
Created:
19-Nov-2006
Python-Version:
3.0
Post-History:


Table of Contents

Abstract

The title says it all – this PEP proposes a newprint() builtinthat replaces theprint statement and suggests a specific signaturefor the new function.

Rationale

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.
  • At some point in application development one quite often feels the needto replaceprint 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.
  • Having special syntax forprint puts up a much larger barrier forevolution, e.g. a hypothetical newprintf() function is not toofar fetched when it will coexist with aprint() function.
  • There’s no easy way to convertprint 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.
  • Ifprint() 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.

Specification

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.

Backwards Compatibility

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

Implementation

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.

References

[1]
http://legacy.python.org/doc/essays/ppt/regrets/PythonRegrets.pdf
[2]
Replacement for print in Python 3.0 (Guido van Rossum)https://mail.python.org/pipermail/python-dev/2005-September/056154.html
[3]
print() parameters in py3k (Guido van Rossum)https://mail.python.org/pipermail/python-3000/2006-November/004485.html

Copyright

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


[8]ページ先頭

©2009-2025 Movatter.jp