This counts for one of the more crazy things I'd like to do with Python: insert an instance of a descriptor object into the class dict of a module.
While you can get hold of the module type class by using the __class__ attribute of any module or use types.ModuleType you obviously can't do this since the __dict__ of the module class is actually a DictProxy and hence immutable. Which I think is rather sad this time round.
My use case is to be able to set an attribute on a module that would lazily evaluate a sort of semi-singleton. Suppose you have the instance of your application and to make it available to other modules you place the instance in a module attribute. You want it to be available since it has references to useful global things like the configuration instance in use etc (and you hate having singletons for all these useful things). To now get this application instance from another module, without getting it passed in with some sort of dependency-injection (which can result in intangible spaghetti all too easily), you can now get hold of it like this:
import app_package.app_module app_package.app_module.app_instance # the instance
But what you very likely can't do is this:
from app_package.app_module import app_instance
The reason is that import statements are usually at the top of modules and thus you will very likely execute this import statement before the app_instance existed and you will get None (I will anyway, since I initialised that attribute with None before the app gets instanced). And obviously even once the application is instanced and the attribute gets set, I'm still stuck with the reference to None instead of the application instance.
So my reason to want to set a descriptor instance in the module class is so that the descriptor could lazily evaluate the application instance when accessing it. Getting and using the instance would reduce to something like:
from app_package.app_module import app_instance app_instance # the instance!
Which I think is much cleaner. But for now I'll have to stick with:
from app_package.app_module import get_instance get_instance() # the instance
Or perhaps Python has another trick up it's sleeve I haven't found yet?