C++-style stream syntax in Python!

1 min read Original article ↗
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