
Sounds more like Thomas is looking for an Pythonic solution without messing around with sys.modules or metaclasses. I personally like the proposals on StackOverflow and don't see why it couldn't be made to work out of the box: class MyClass: @classproperty def prop(cls): return '42' # module property @property def prop(mod): return '42' On 20.08.2015 09:47, Nathaniel Smith wrote:
On Thu, Aug 20, 2015 at 12:20 AM, Andrew Barnert via Python-ideas <python-ideas@python.org> wrote:
Modules are a more serious problem, because there's no immediate way to specify the type for a module object. You can work around it by, e.g., declaring a module subclass, copying the module's dict to an instance of that subclass, then replacing the entry in sys.modules, but this is more than a little ugly.
There have been proposals to allow modules to specify a type (e.g., something similar to the way __metaclass__ worked in 2.x), and to make it easier to hook the import machinery to create modules of a custom type, and probably other variations on this. You might want to search this list and -dev for previous ideas, to find one you think should be reconsidered. This is already done :-). Python 3.5 allows a module to contain code like:
class MetaModule(types.ModuleType): @property def some_attribute(self): print("Accessing module.some_attribute") return 1
sys.modules[__name__].__class__ = MetaModule
See also
https://pypi.python.org/pypi/metamodule
for utility code and a polyfill back to ancient Pythons.
-n