Loading just in time
Scott David Daniels
Scott.Daniels at Acm.Org
Thu Jul 10 23:24:38 EDT 2008
D'Arcy J.M. Cain wrote:
> I am trying to create a utility module that only loads functions when
> they are first called rather than loading everything. I have a bunch
> of files in my utility directory with individual methods and for each I
> have lines like this in __init__.py:
>
> def calc_tax(*arg, **name):
> from calc_tax import calc_tax as _func_
> calc_tax = _func_
> return _func_(*arg, **name)
> ...
Well, you may well be "mired in premature optimization," but you _could_
do the following (you'll need to evaluate whether it addresses your
issues).
def calc_tax(*arg, **kwargs):
from calc_tax import calc_tax
globals()['calc_tax'] = calc_tax
return calc_tax(*arg, **kwargs)
But if you are terrifyingly clever, (or have enough motive*), you can
define a function, '_remote', So that your __init__.py contains:
<some magic>
...
_remote('some_tax')
_remote('other', 'frogs')
...
* The pythonic "enough motive" is how redundant your "__init__.py"
looks. The "DRY" (Don't Repeat Yourself) principle comes into effect.
The more your source looks like boilerplate, the more you should itch
to replace the boilerplate.
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list