[Python-3000] Interest in PEP for callbacks on module import

Christian Heimes lists at cheimes.de
Mon Dec 10 02:35:42 CET 2007


Jim Jewett wrote:
> Could you give me a use-case for post-import hooks *without* lazy imports?

This thread was started with perfect valid and good use case. We want to
apply ABCs to classes of optional modules like decimal and array without
loading the module or modifying the module.

   @imp.imported('decimal')
   def register(decimal):
       Inexact.register(decimal.Decimal)

Lazy imports won't do the trick because the register could would load
the real module. As you can see the problem can be solved with a post
import hook but *not* with lazy imports.

> As nearly as I can tell, either
> 
> (1)  The module is already loaded (and the whole infrastructure is overhead)
> 
> (2a)  You will be importing the module.  Then you can just run your
> hooks directly without any infrastructure.
> 
> (2b)  You will not be importing the module you need to hook -- in
> which case I think you want laziness as well.

(3) You want to run your hook in order to modify or register
    the contents module when the user imports the module.

By the way I've started to work on an implementation:
http://bugs.python.org/issue1576

Christian


More information about the Python-3000 mailing list