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__


-- 
Steve
_______________________________________________
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/2BNQFPWLNBF37XXCU6RYCA5Q7PWZQEXT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to