I have certainly overridden sys.stdout, it's USEFUL, Shreyan.  (One use case was to make programs pause after each screenful of output. Probably I've also used it to suppress output.)
But I didn't know about sys.__stdout__, thanks Steven.
And yes, I'm at least as confused as to what the proposal is as anybody else.
Rob Cliffe

On 26/05/2021 14:20, Steven D'Aprano wrote:
On Wed, May 26, 2021 at 12:53:32PM -0000, Shreyan Avigyan wrote:
I've already given one. Since Python is dynamically typed changing a critical 
variable can cause huge instability. Want a demonstration? Here we go,

import sys
sys.stdout = None

Now what? Now how can we print anything? Isn't this a bug?
Assigning to None is probably a TypeError, because None doesn't
implement the file object interface. But assigning to a file is
perfectly correct.

stdout, stdin and stderr are designed to be assigned to.

That's why the sys module defines `sys.__stdout__` etc, so you can
easily restore them to the originals.

You should also note that there are many circumstances where sys.stdout
etc are all set to None. See the documentation.

https://docs.python.org/3/library/sys.html#sys.__stdin__



_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/RK74MBNQEL7NIU4ZGWK6Z2T5OTX43YX4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to