Created
October 20, 2017 17:26
-
-
Save ilyanep/496d8b3643e6e1e8755a45c235275aa0 to your computer and use it in GitHub Desktop.
C++-style stream syntax in Python!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
# Inspired by a friend who hadn't had her coffee yet and thought | |
# she was writing C++ when she was in a Python file. | |
class ostream: | |
def __init__(self, target): | |
self.target = target | |
def __lshift__(self, other): | |
self.target.write(other) | |
return self | |
cout = ostream(sys.stdout) | |
endl = "\n" | |
cout << "foo" << endl | |
myFile = ostream(open("foo.txt", "w")) | |
myFile << "foo" << endl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not proud of myself