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.