Overriding "__setattr__" of a module - possible?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Wed Jun 16 08:08:41 EDT 2010


On Tue, 15 Jun 2010 20:34:47 -0700, Michele Simionato wrote:

> On Jun 16, 4:43 am, John Nagle <na... at animats.com> wrote:
>>    Is it possible to override "__setattr__" of a module?  I
>> want to capture changes to global variables for debug purposes.
[...]
> There is a dirty trick which involves fiddling with sys.modules. For
> instance:
[snip example of installing a class in sys.modules]

I'm not sure that this is a dirty trick. Isn't it officially supported? 
If I recall correctly, Python used to insist that modules in sys.modules 
were actual module objects, and that restriction was lifted deliberately 
to allow clever tricks like this. Or am I thinking of something else?


-- 
Steven



More information about the Python-list mailing list