PEP 214 completed after channeling and encouragement from Tim Peters.

The referenced SF patch is current with this description.

The proposal section is moved to before the justification.
This commit is contained in:
Barry Warsaw 2000-08-16 14:59:57 +00:00
parent 5a39c68258
commit 7bbbe401fa
1 changed files with 36 additions and 51 deletions

View File

@ -2,10 +2,10 @@ PEP: 214
Title: Extended Print Statement Title: Extended Print Statement
Version: $Revision$ Version: $Revision$
Author: bwarsaw@beopen.com (Barry A. Warsaw) Author: bwarsaw@beopen.com (Barry A. Warsaw)
Python-Version: 2.1 Python-Version: 2.0
Status: Draft Status: Draft
Created: 24-Jul-2000 Created: 24-Jul-2000
Post-History: Post-History: 16-Aug-2000
Introduction Introduction
@ -21,6 +21,37 @@ Introduction
record. record.
Proposal
This proposal introduces a syntax extension to the print
statement, which allows the programmer to optionally specify the
output file target. An example usage is as follows:
print >> mylogfile, 'this message goes to my log file'
Formally, the syntax of the extended print statement is
print_stmt: ... | '>>' test [ (',' test)+ [','] ] )
where the ellipsis indicates the original print_stmt syntax
unchanged. In the extended form, the expression just after >>
must yield an object with a write() method (i.e. a file-like
object). Thus these two statements are equivalent:
print 'hello world'
print >> sys.stdout, 'hello world'
As are these two statements:
print
print >> sys.stdout
These two statements are syntax errors:
print ,
print >> sys.stdout,
Justification Justification
`print' is a Python keyword and introduces the print statement as `print' is a Python keyword and introduces the print statement as
@ -40,12 +71,11 @@ Justification
method, but it can be rebound to redirect output to files other method, but it can be rebound to redirect output to files other
than specifically standard output. A typical idiom is than specifically standard output. A typical idiom is
oldstdout = sys.stdout
sys.stdout = mylogfile sys.stdout = mylogfile
try: try:
print 'this message goes to my log file' print 'this message goes to my log file'
finally: finally:
sys.stdout = oldstdout sys.stdout = sys.__stdout__
The problem with this approach is that the binding is global, and The problem with this approach is that the binding is global, and
so affects every statement inside the try: clause. For example, so affects every statement inside the try: clause. For example,
@ -53,53 +83,8 @@ Justification
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 This approach is also very inconvenient for interleaving prints to
various output streams. various output streams, and complicates coding in the face of
legitimate try/except or try/finally clauses.
Proposal
This proposal introduces a syntax change to the print statement,
which allows the programmer to optionally specify the output file
target. An example usage is as follows:
print >> mylogfile, 'this message goes to my log file'
Formally, the syntax of the extended print statement is
print_stmt: "print" [">>" expr ","] [ expr ("," expr)* [","] ]
Where the the expression just after >> must yield an object with a
write() method (i.e. a file-like object). Thus these two
statements are equivalent:
print '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