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

Christian Heimes lists at cheimes.de
Mon Dec 10 17:04:22 CET 2007


Jim Jewett wrote:
 > I had thought that wouldn't be run until the module was otherwise
> loaded.  Are you saying that just creating the callback forces a load
> under lazy loading, but not under post-load hooks?

The imp.imported (or whatever we name the method) is the register method
for the post import hook system. It's not connected to lazy imports
directly. Internally lazy imports and post import hooks work together
but that's an implementation detail.

I was saying that the problem can't be solved with lazy imports. Here is
an example in doc test style how lazy imports and post import hooks
would work together:

Register a hook
>>> def hook(mod):
...     print "Module loaded"
>>> imp.register_post_import_hook(hook, "decimal")

Load the decimal module lazy.
>>> decimal = imp.importlazy("decimal")

decimal is a lazy module. Any attribute access will load the real
decimal module.
>>> imp.islazy(decimal)
True

Registering the decimal.Decimal class with the Inexact ABC loads the
real decimal module.
>>> Inexact.register(decimal.Decimal)
Module loaded
>>> imp.islazy(decimal)
False

Christian


More information about the Python-3000 mailing list