Overriding "__setattr__" of a module - possible?

Stephen Hansen me+list/python at ixokai.io
Wed Jun 16 00:33:19 EDT 2010


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.	

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100615/9434fc2b/attachment-0001.sig>


More information about the Python-list mailing list