Updated discussion, including two open issues. I've also changed the

Python-Version to 2.1 since this probably needs more discussion,
unless the BDFL pronounces favorably on it.
This commit is contained in:
Barry Warsaw 2000-08-15 22:45:06 +00:00
parent 99488174df
commit 44c088252b
1 changed files with 37 additions and 22 deletions

View File

@ -1,12 +1,13 @@
PEP: 214 PEP: 214
Title: Extended Print Statement Title: Extended Print Statement
Version: $Revision$ Version: $Revision$
Owner: bwarsaw@beopen.com (Barry A. Warsaw) Author: bwarsaw@beopen.com (Barry A. Warsaw)
Python-Version: 2.0 Python-Version: 2.1
Status: Draft Status: Draft
Created: 24-Jul-2000
Post-History:
Introduction Introduction
This PEP describes a syntax to extend the standard `print' This PEP describes a syntax to extend the standard `print'
@ -20,7 +21,6 @@ Introduction
record. record.
Justification Justification
`print' is a Python keyword and introduces the print statement as `print' is a Python keyword and introduces the print statement as
@ -52,8 +52,10 @@ Justification
if we added a call to a function that actually did want to print if we added a call to a function that actually did want to print
to stdout, this output too would get redirected to the logfile. to stdout, this output too would get redirected to the logfile.
This approach is also very inconvenient for interleaving prints to
various output streams.
Proposal Proposal
This proposal introduces a syntax change to the print statement, This proposal introduces a syntax change to the print statement,
@ -74,28 +76,42 @@ Proposal
print >> sys.stdout, 'hello world' print >> sys.stdout, 'hello world'
Open Issues
What should the following do?
print >> file
print >> file,
In the current implementation (see below), the first is a
SyntaxError and the second prints nothing to file. This is likely
counterintuitive; the first should print just a newline, making
these equivalent:
print >> sys.stdout
print
The second should print just a space and no newline to file. It
doesn't have a non-extended print equivalent, since this is
illegal:
print ,
The closes equivalent is:
print '',
Reference Implementation Reference Implementation
A reference implementation, in the form of a patch against the A reference implementation, in the form of a patch against the
Python 2.0 source tree, is available on SourceForge's patch Python 2.0 source tree, is available on SourceForge's patch
manager[2]. The approach this patch takes is to introduce two new manager[2]. This approach adds two new opcodes, PRINT_ITEM_TO and
opcodes, one which temporarily rebinds sys.stdout to the specified PRINT_NEWLINE_TO, which simply pop the file like object off the
file object, performs the print as normal, and then bind top of the stack and use it instead of sys.stdout as the output
sys.stdout back to sys.__stdout__ (which is the real physical stream.
standard out and should not be changed). In some ways this is
equivalent to the try/finally idiom above, except that the
rebinding of sys.stdout is in effect only for the duration of the
print statement itself.
An alternative approach is possible, where only one new opcode is
added. This opcode would be exactly like the existing PRINT_ITEM
opcode except that it would find the target file object at the top
of the stack, and use this file instead of digging it out of
sys.stdout.
Alternative Approaches Alternative Approaches
An alternative to this syntax change has been proposed (originally An alternative to this syntax change has been proposed (originally
@ -139,7 +155,6 @@ Alternative Approaches
in between each item. in between each item.
References References
[1] http://www.python.org/doc/current/ref/print.html [1] http://www.python.org/doc/current/ref/print.html