Define type of 'module' object that is imported

robert no-spam at no-spam-no-spam.com
Sun Apr 23 08:04:34 EDT 2006


Zachary Pincus wrote:
> 
> No, just one particular module that needs to do some slow things.
> I'd like for a user to just be able to type
> "import foo"
> and have the foo module's type be some special type. None of the  
> options below really allow this to happen with one step. It would be  
> more like "import lazyFoo; import foo".
> 
> What I'm doing now is in 'foo.py' performing surgery a la:
> sys.modules[__name__] = MyModuleClass(...)
> but that's ugly, and can break the reload mechanism, and causes some  
> other troubles.

modules must go to sys.modules. reloading would have to be prevented by 
simple test. yet if your module is ready no-one would reload.

yet executing the module for reload in that trick namespace will also do 
  to a certain degree.

you could also try to replace / update the original modules's __dict__

you still didn't mention the original motivation for that 
lazy-moduletype replacement.
If its your module anyway, you could (auto-)rework your functions/class 
  to do delayed initializations at the end of the module.

something like:

for func in <iter-certain-funcs-in-globals()>:
      globals()[func_name]=hook_lazy_init(func)


-robert



More information about the Python-list mailing list