[Python-checkins] CVS: python/nondist/peps pep-0214.txt,1.2,1.3
Barry Warsaw
python-dev@python.org
Wed, 16 Aug 2000 08:00:00 -0700
Update of /cvsroot/python/python/nondist/peps
In directory slayer.i.sourceforge.net:/tmp/cvs-serv3071
Modified Files:
pep-0214.txt
Log Message:
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.
Index: pep-0214.txt
===================================================================
RCS file: /cvsroot/python/python/nondist/peps/pep-0214.txt,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** pep-0214.txt 2000/08/15 22:45:06 1.2
--- pep-0214.txt 2000/08/16 14:59:57 1.3
***************
*** 3,10 ****
Version: $Revision$
Author: bwarsaw@beopen.com (Barry A. Warsaw)
! Python-Version: 2.1
Status: Draft
Created: 24-Jul-2000
! Post-History:
--- 3,10 ----
Version: $Revision$
Author: bwarsaw@beopen.com (Barry A. Warsaw)
! Python-Version: 2.0
Status: Draft
Created: 24-Jul-2000
! Post-History: 16-Aug-2000
***************
*** 22,25 ****
--- 22,56 ----
+ 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
***************
*** 41,50 ****
than specifically standard output. A typical idiom is
- oldstdout = sys.stdout
sys.stdout = mylogfile
try:
print 'this message goes to my log file'
finally:
! sys.stdout = oldstdout
The problem with this approach is that the binding is global, and
--- 72,80 ----
than specifically standard output. A typical idiom is
sys.stdout = mylogfile
try:
print 'this message goes to my log file'
finally:
! sys.stdout = sys.__stdout__
The problem with this approach is that the binding is global, and
***************
*** 54,105 ****
This approach is also very inconvenient for interleaving prints to
! various output streams.
!
!
! 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
--- 84,90 ----
This approach is also very inconvenient for interleaving prints to
! various output streams, and complicates coding in the face of
! legitimate try/except or try/finally clauses.
Reference Implementation