1

I know that in python I can overload method getattribute of any class to intercept accesses to its attributes (if those attributes exist or if they don't).

Since modules are objects too, I wonder how to do that with module variables.

I tried creating a module like this:

def foo(_, name):
    print 'Fetching attribute ' + name
    return object.__getattribute__(name)

globals()['__getattribute__'] = foo

import sys
sys.modules[__name__].__dict__['__getattribute__'] = foo

my_module_var = 9

But when I import it and access to my_module.my_module_var, the "interceptor" I tried to set up was not called.

juanleon
  • 9,220
  • 30
  • 41

0 Answers0