Ready to bring some flavor of C++ into python? If you like cout, cin in C++ and also a python programmer, you’d definitely like this snippet.
import sys
class ostream:
def __init__(self, file):
self.file = file
def __lshift__(self, obj):
self.file.write(str(obj));
return self
cout = ostream(sys.stdout)
cerr = ostream(sys.stderr)
endl = 'n'
x, y = 'Printing', 'like C++'
cout << x << " " << y << endl
I found this and a lot of other cool stuffs from Peter Norvig’s blog. I highly recommended subscribing to this blog.
1 Comment to “Python: C++ style cin, cout in Python”
Post comment
Search
News
Blog
- Fetching data from multiple tables in one MySQL query – Part 2
- Fetching data from multiple tables in one MySQL query
- Let’s Go A Hunting
- Java Applet and Javascript message passing
- The Right-Click Menu in Flash
- Stroke hinting
- Python: C++ style cin, cout in Python
- Python: Working in Unicode
- Website Security : Directory Listing Issue
- Hyperlink in actionscript 3


June 25, 2009 in

July 10, 2009 at 9:11 pm
Where is your cin example?