Skip to content

Instantly share code, notes, and snippets.

@ilyanep
Created October 20, 2017 17:26
Show Gist options
  • Save ilyanep/496d8b3643e6e1e8755a45c235275aa0 to your computer and use it in GitHub Desktop.
Save ilyanep/496d8b3643e6e1e8755a45c235275aa0 to your computer and use it in GitHub Desktop.
C++-style stream syntax in Python!
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
@ilyanep
Copy link
Author

ilyanep commented Oct 20, 2017

I'm not proud of myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment