Blog
1

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”

  • Where is your cin example?

Post comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>