This class is used to intercept an unset attribute of a module to perfrom a callback. The callback will only be performed if the attribute does not exist on the module. Any error raised in the callback will cause the original AttributeError to be raised.
- def cb( module, attr):
- if attr == ‘this’:
- print “intercepted”
- else:
- raise ValueError
import sys sys.modules[__name__] = ModuleInterceptor(__name__, cb) intercepted
The class does not work when imported into the main namespace.