You can't do that for ordinary modules, since their getattr() is fixed
and requires that the name is already defined in the dictionary.
However you can do it with object types you define yourself (and I
seem to understand that you are already doing that). I see two ways
of exploiting this for the khoros module:
- Instead of making these functions part of the module, add a magic
object to the module and make them methods of that object -- it can
then do the lazy import. Then tell your users to use methods of this
object.
- In the initialization of the module, manipulate sys.modules so that
sys.modules['khoros'] is not a module but a special object as in the
previous item (there may be a line in add_module that prevents this
from working -- I'm willing to take it out).
BTW, when you ask Khoros "does a function named kflip exist", does it
also tell you what the parameter types are? Or do all functions have
the same parameter list, or is there another way to tell?
--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>