On 2021-05-26 13:53, 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? There are lots of code out there where we need to protect things from being overwritten. Though I'm never telling to use constants in Python stdlib or else I could have never done this demonstration. :)
Actually, I've done something like that.
import codecs import sys sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
This was because I was running a script from an editor and it picked up the wrong output encoding, raising an error when I tried to print characters that were outside the ASCII range. Thankfully that problem is now solved, but it was a useful workaround.