python sys.stdout and C++ iostreams::cout
问题 I was assuming that sys.stdout would be referencing the same physical stream as iostreams::cout running in the same process, but this doesn't seem to be the case. The following code, which makes a call to a C++ function with a python wrapper called "write", that writes to cout: from cStringIO import StringIO import sys orig_stdout = sys.stdout sys.stdout = stringout = StringIO() write("cout") # wrapped C++ function that writes to cout print "-" * 40 print "stdout" sys.stdout = orig_stdout