On Monday 07 March 2005 11:52, Claudio Grondi wrote: > I try to avoid using any of the > __xxxx__() functions if possible > (considering this a good > programming style).
This is never good style, at least in the case of exec. exec is evil. What works (beware that the below code is nevertheless untested and might contain little warts) and is the "usual" and clean way to go: ### libinfo/__init__.py # Empty. ### libinfo/Module1.py def libinfo(): return "I am module1." CFLAGS = ["-DMODULE1"] ### libinfo/Module2.py def libinfo(): return "I am module2." CFLAGS = ["-DMODULE2"] ### Importer.py modules = {} CFLAGS = [] def load_modules(to_load=["module1","module2"]): global modules, CFLAGS for mod in to_load: try: modules[mod] = getattr(__import__("libinfo.%s" % mod),mod) except ImportError: print "Could not load %s." % mod continue print "Module: %s (%r)." % (mod,modules[mod]) print "Modules libinfo: %r." % modules[mod].libinfo() CFLAGS += modules[mod].CFLAGS print "Total CFLAGS: %s." % CFLAGS if __name__ == "__main__": load_modules() print "Module container: %s." % modules ### End Importer.py HTH! -- --- Heiko.
pgpfjzzhE3U5B.pgp
Description: PGP signature
-- http://mail.python.org/mailman/listinfo/python-list