AttributeError of a module instance
holger krekel
hpk at trillke.net
Fri Jan 7 19:14:11 EST 2005
On Mon, Dec 26, 2005 at 17:41 +0000, Paolino wrote:
> I'd like to catch AttributeError on the module level,so that I can
> declare default bindings for useds defore definition.How is this to be
> done?Thanks for help.
It cannot be done directly but with a small hack. This is
the idea:
import sys
class A:
def __getattr__(self,name):
if name[:1] != '_':
return name
raise AttributeError(name)
sys.modules['a'] = A()
import a
print a.helloworld # will print "helloworld"
Note, that subsequently you can "import a" also
from other modules because the import statement
consults sys.modules.
This is all a bit bothersome and thus it's often
easier to just do "from somemod import a" directly
and forget about the above magic.
cheers,
holger
More information about the Python-list
mailing list