The problem is simple. I copied an example from a Python book about output redirection and firstly sys.stdout
is changed to a file object and then is restored to sys.__stdout__
.
However the restoration part doesn't seem to work because the output later in the code doesn't get displayed at all anywhere.
I found out the issue which I'll explain with this shell code sample:
>>> import sys
>>> sys.stdout
<idlelib.run.StdOutputFile object at 0x00000191D0BCFB50>
>>> sys.__stdout__
>>> print(sys.__stdout__)
None
>>>
So: why is sys.__stdout__
equal to None
by default?
EDIT: I know about the context manager in contextlib explained in this issue but I still would like to know why this happens.