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:
parent
99488174df
commit
44c088252b
59
pep-0214.txt
59
pep-0214.txt
|
@ -1,12 +1,13 @@
|
|||
PEP: 214
|
||||
Title: Extended Print Statement
|
||||
Version: $Revision$
|
||||
Owner: bwarsaw@beopen.com (Barry A. Warsaw)
|
||||
Python-Version: 2.0
|
||||
Author: bwarsaw@beopen.com (Barry A. Warsaw)
|
||||
Python-Version: 2.1
|
||||
Status: Draft
|
||||
Created: 24-Jul-2000
|
||||
Post-History:
|
||||
|
||||
|
||||
|
||||
Introduction
|
||||
|
||||
This PEP describes a syntax to extend the standard `print'
|
||||
|
@ -20,7 +21,6 @@ Introduction
|
|||
record.
|
||||
|
||||
|
||||
|
||||
Justification
|
||||
|
||||
`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
|
||||
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
|
||||
|
||||
This proposal introduces a syntax change to the print statement,
|
||||
|
@ -74,28 +76,42 @@ Proposal
|
|||
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
|
||||
|
||||
A reference implementation, in the form of a patch against the
|
||||
Python 2.0 source tree, is available on SourceForge's patch
|
||||
manager[2]. The approach this patch takes is to introduce two new
|
||||
opcodes, one which temporarily rebinds sys.stdout to the specified
|
||||
file object, performs the print as normal, and then bind
|
||||
sys.stdout back to sys.__stdout__ (which is the real physical
|
||||
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.
|
||||
manager[2]. This approach adds two new opcodes, PRINT_ITEM_TO and
|
||||
PRINT_NEWLINE_TO, which simply pop the file like object off the
|
||||
top of the stack and use it instead of sys.stdout as the output
|
||||
stream.
|
||||
|
||||
|
||||
|
||||
Alternative Approaches
|
||||
|
||||
An alternative to this syntax change has been proposed (originally
|
||||
|
@ -139,7 +155,6 @@ Alternative Approaches
|
|||
in between each item.
|
||||
|
||||
|
||||
|
||||
References
|
||||
|
||||
[1] http://www.python.org/doc/current/ref/print.html
|
||||
|
|
Loading…
Reference in New Issue