i can silence and restore sys.stdout
this way:
import sys
sys.stdout = None
print('hello') # does not write to stdout
sys.stdout = sys.__stdout__
print('hello') # writes to stdout
i know i'd better be using contextlib.redirect_stdout
which probably does something similar but my question is: why does the above code work?
i'd have assumed python would call things like sys.stdout.write()
so whatever i replace sys.stdout
with should have a write
method (like e.g. io.StringIO
) at least.