[Python-3000] Adaptation vs. Generic Functions

Phillip J. Eby pje at telecommunity.com
Thu Apr 13 19:13:01 CEST 2006

At 10:34 AM 4/13/2006 -0500, Ian Bicking <ianb at colorstudy.com> wrote:
>And even if that wasn't a problem, it means mpz would be eagerly loaded
>along with NumPy even if it wasn't needed.  Or if you had to manually
>import this operator.add-glue, then if you forget it is likely to be
>difficult to debug, because there's no *bad* code, just code that is

PEAK addresses this problem with a decorator:

     def setup_for_ mpz(mpz):
         def ...

The 'whenImported' function checks to see if mpz is already present in 
sys.modules.  If so, it calls the function immediately, passing in the 
named module.  If not, it puts an object in sys.modules whose 
__getattribute__ tries to actually import the module and then calls back 
any registered functions, so that anybody else importing mpz will get the 
real module.

I've actually been using this for a few years now, as the eager-vs.-lazy 
problem is independent of setuptools and package activation.  It comes up 
any time you want to offer support for something without creating a 
dependency on it.

More information about the Python-3000 mailing list