Overriding "__setattr__" of a module - possible?

John Nagle nagle at animats.com
Wed Jun 16 01:25:50 EDT 2010


On 6/15/2010 9:33 PM, Stephen Hansen wrote:
> On 6/15/10 9:16 PM, John Nagle wrote:
>>     Cute, but it doesn't work in general.  Faking a module as a
>> class fails when you simply call
>>
>>      x()
>>
>> within the module.
>
> Huh? Explain how it doesn't work? I've done it at least twice
> (shamefully do I admit this) and it works fine. Real life code.
>
>> Any more ideas?
>
> There aren't any; modules do not follow the class object protocol. They
> are simple types with a __dict__ (which you can't change, either, so no
> replacing it with a dict that implements __setattr__).
>
> Replacing the module with a class in sys.modules is really the only way
> to fake the behavior.	

    OK, working on this.  I can make a module make itself into a
fake class, but can't yet do it to other modules from outside.

					John Nagle




More information about the Python-list mailing list