Overriding "__setattr__" of a module - possible?
Lie Ryan
lie.1296 at gmail.com
Wed Jun 16 07:26:23 EDT 2010
On 06/16/10 12:43, John Nagle wrote:
> Is it possible to override "__setattr__" of a module? I
> want to capture changes to global variables for debug purposes.
>
> None of the following seem to have any effect.
>
> modu.__setattr__ = myfn
>
> setattr(modu, "__setattr__", myfn)
>
> delattr(modu, "__setattr__")
>
> John Nagle
I doubt this is what you wanted, but...
import itertools
module = type(itertools)
class FakeModule(module):
def __init__(self, module):
super(FakeModule, self).__init__(module.__name__, module.__doc__)
self.__module = module
def __getattr__(self, attr):
return getattr(self.__module, attr)
def __setattr__(self, attr, val):
print self, attr, val
super(FakeModule, self).__setattr__(attr, val)
More information about the Python-list
mailing list