PEP 726: Module __setattr__ and __delattr__

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 own warnings.catch_warnings() functions does self._module.filters = self._filters to restore the attribute to its previous value.

There are some constants which may be interesting to implement as constants: either deny modifying them, or at least disallow removing them. Examples:

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? :wink: