importing symbols from module objects

Paul Rubin phr-n2001 at nightsong.com
Tue Sep 25 20:59:28 EDT 2001


Is there a non-messy way to import a symbol from a module object?
I don't see one but I might be missing something.  ("Non-messy"
means doesn't include any double underscores or call weird internals
routines).

Example: I have two different implementations of the "breakfast" class,
EggsBaconSpam.py and SpamEggsSausageSpam.py.  Each of them can throw
SpamExceptions.  The calling code wants to import SpamException into
its own namespace, because it catches a lot of them.

I'd like to let the caller say something like:

   import EggsBaconSpam as breakfastdish
   ...
   bk = breakfastdish.breakfast(eggs=3)

The idea is to be able to change the import statement to
   import SpamEggsSausageSpam as breakfastdish

without having to change the places that call it.  This is called a
"service provider" API.  The question is how to import symbols from the
service provider.  The caller doesn't seem to be able to say

   from breakfastdish import SpamException 

to import the symbol from the already-loaded breakfastdish object
instead of having to say

  from EggsBaconSpam import SpamException 

which makes an additional place the caller needs to change
in order to switch service providers.  Any suggestions?

Thanks



More information about the Python-list mailing list