In Python, what's the best way to implement a `@cached_module_property`?

There are a lot of things that can be done with classes that don’t work directly with modules.

One common hack is to have the module replace itself with a class instance in sys.modules on import. Something like (I didn’t test this, but I know I’ve seen similar before):

from functools import cache
from time import sleep

class _implementation:
    @cache
    @property
    def value(self):
        time.sleep(10)
        return 'value'

if __name__ != '__main__' and not isinstance(sys.modules[__name__], _implementation):
    sys.modules[__name__] = _implementation()