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.