# Extend PEP 562 with \_\_setattr\_\_ for modules?

**URL:** <https://discuss.python.org/t/extend-pep-562-with-setattr-for-modules/25506>\
**Category:** Ideas\
**Created:** [April 6, 2023, 5:26am UTC](https://discuss.python.org/t/extend-pep-562-with-setattr-for-modules/25506 "2023-04-06T05:26:16Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![skirpichev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/skirpichev/32/10996_2.png) [@skirpichev](https://discuss.python.org/u/skirpichev)\
**Post date:** [April 6, 2023, 5:26am UTC](https://discuss.python.org/t/extend-pep-562-with-setattr-for-modules/25506/1 "2023-04-06T05:26:16Z")

</div>

Since CPython 3.5 it’s possible to customize setting module attributes by setting ` __class__ ` attribute. Unfortunately, this coming with a measurable speed regression for attribute access:

```auto
$ cat b.py
x = 1
$ python -m timeit -r11 -s 'import b' 'b.x'
5000000 loops, best of 11: 48.8 nsec per loop
$ cat c.py
import sys, types
x = 1
class _Foo(types.ModuleType): pass
sys.modules[__name__]. __class__ = _Foo
$ python -m timeit -r11 -s 'import c' 'c.x'
2000000 loops, best of 11: 131 nsec per loop

```

For reading attributes this could be workarounded with ` __getattr__ `, but there is no similar support for ` __setattr__ `. Maybe it does make sense as well (or even for ` __delattr__ `)? If so, will this require  
a PEP? (Draft implementation is in [GitHub - skirpichev/cpython at module-setattr](https://github.com/skirpichev/cpython/tree/module-setattr))

As a background story, this solution come from the mpmath issue [ENH: guard against incorrect use of `mp.dps`? · Issue #657 · mpmath/mpmath · GitHub](https://github.com/mpmath/mpmath/issues/657) In short, people are trying to set precision attributes not on the context object (mpmath.mp, e.g. mpmath.mp.dps), but on the mpmath module. Probably, this is a very special scenario, but…

---

_[View the full topic](https://discuss.python.org/t/extend-pep-562-with-setattr-for-modules/25506)._
