Replacing a built-in method of a module object instance
davidh at ilm.com
Fri Jun 26 20:09:53 EDT 2009
I have a need to replace one of the built-in methods of an arbitrary
instance of a module in some python code I'm writing.
Specifically, I want to replace the __getattribute__() method of the
module I'm handed with my own __getattribute__() method which will do
some special work on the attribute before letting the normal attribute
I'm not sure how this would be done. I've looked at all the
documentation on customizing classes and creating instance methods...but
I think I'm missing something about how built-in methods are defined for
built-in types, and where I'd have to replace it. I tried this naive
approach, which doesn't work:
m = <module instance>
def __getattribute__(self, attr):
print "modified getattribute:",attr
return object.__getattribute__(self, attr)
m.__getattribute__ = types.MethodType(__getattribute__,m)
It seems to create an appropriately named method on the module instance,
but that method isn't called when doing any attribute lookups, so
something's not right.
Any ideas? Is this even possible?
Thanks in advance!
More information about the Python-list