importing symbols from module objects

Sean 'Shaleh' Perry shalehperry at home.com
Tue Sep 25 21:19:35 EDT 2001


On 26-Sep-2001 Paul Rubin wrote:
> 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).
> 

when you try to get around the language, "weird" calls is all you have.  You
example may not truly reflect what you are trying to do because:

from EggsBaconSpam import breakfast, SpamException

gives you what you want and has only the one line to change.

In some of my own code I have:

d = {}
exec open(py) in d
self.unpack.register(d['filetype'], d['unpack'])

'py' is the name of a python module.  the exec call places py's symbols in
dictionary d, then you just use the dictionary to access the
functions/classes/etc.  I use this to dynamically load modules which all define
the same functions, in this case 'unpack' is a function name and 'filetype' is
either a test snippet or truth function.




More information about the Python-list mailing list