Shreyan Avigyan writes: > 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?
With print(). Even in your code, it still works, for an appropriate definition of "works". See next comment. > Isn't this a bug? Maybe, but if it is, it's a bug because you didn't sys.stdout.close() before rebinding it.[1] sys.stdout = None is useful; it has the effect of sys.stdout = open("/dev/null", "w") ie, shutting up a chatty program. (This surprised me; I thought print would raise because None has no 'write' attribute. It's an 'is None' test, not a bool test; it does complain on sys.stdout = 0. It really doesn't like 0, it complains again -- about 'flush' -- on interpreter exit. :-) > There are lots of code out there where we need to protect things > from being overwritten. Perhaps you could give a more persuasive example. The whole idea is against the usual "consenting adults" approach of Python. Or as that famous Python programmer Barack Obama put it, "let's try not to do stupid shit." :-) Footnotes: [1] I suspect there's a race condition in some environments. At least up to version 9, MacOS poll(2) and X.org were buggy together. The event loop would poll(), and if you had an open file descriptor that hadn't been closed before fork but was somehow unused in the child, it would infloop on an OS error from poll(). So if you fork before sys.stdout gets GC'd and therefore .close()'d, you could end up with such a zombie fd. _______________________________________________ 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/QNRFTDB65VFJ6LWSJFGNIV2EZCFBM2DW/ Code of Conduct: http://python.org/psf/codeofconduct/