The sys
module API is designed around the idea of setting its attribute, but there are other stdlib modules with some attributes which can be overriden (and it’s explained in their doc, it’s the way to go).
Some more examples:
-
threading.excepthook doc: can be overridden to control how uncaught exceptions raised by Thread.run() are handled.
-
warnings
- warnings.showwarning() doc: You may replace this function with any callable by assigning to
warnings.showwarning
warnings.filters
is not documented (!), but it’s commonly modified. Its ownwarnings.catch_warnings()
functions doesself._module.filters = self._filters
to restore the attribute to its previous value.
- warnings.showwarning() doc: You may replace this function with any callable by assigning to
There are some constants which may be interesting to implement as constants: either deny modifying them, or at least disallow removing them. Examples:
- os.sep and curdir, pardir, altsep, extsep, defpath, linesep, …
- os.environ: is it a feature to be able to override the attribute? or a bug?
- math.pi and e, tau, inf, nan
- http.client.HTTP_PORT and HTTPS_PORT
I’m not declaring a war to monkey patching (even if I not really like that). If the PEP is approved, each of these attributes should be discussed on a case by case basis.
On the other side, it seems like io.DEFAULT_BUFFER_SIZE can be modified on purpose. Just don’t assign it to a string, right?