Emulating C++ coding style
Steven D. Majewski
sdm7g at Virginia.EDU
Thu Apr 22 20:54:13 EDT 1999
On Fri, 23 Apr 1999 06:08:07 +0900, Thooney Millennier asked about
Python equivalents to C++ code.
>
> >2. stream class
> > e.g. cout << "hello python."<<endl;
On Thu, 22 Apr 1999, William Tanksley replied:
> VERY bad idea. Bad even in C++.
>
> sys.stdout.write("hello python." + "\n");
However, if you *REALLY* want to have that sort of C++ syntax,
you can get it in Python with something like:
endl = '\n'
class OutStream:
def __init__( self, file ):
if hasattr( file, 'write' ):
self.file = file
else:
self.file = open( file, 'w' )
def write( self, what ):
self.file.write( what )
def close( self ):
self.file.close()
def __lshift__(self, what ):
self.write( str(what)+' ')
return self
import sys
out = OutStream( sys.stdout )
out << "Hello" << "World" << endl
out << "The answer is" << 3+4 << endl
---| Steven D. Majewski (804-982-0831) <sdm7g at Virginia.EDU> |---
---| Department of Molecular Physiology and Biological Physics |---
---| University of Virginia Health Sciences Center |---
---| P.O. Box 10011 Charlottesville, VA 22906-0011 |---
Caldera Open Linux: "Powerful and easy to use!" -- Microsoft(*)
(*) <http://www.pathfinder.com/fortune/1999/03/01/mic.html>
More information about the Python-list
mailing list