PyWart: Python modules are not so "modular" after all!

Steven D'Aprano steve at pearwood.info
Mon Nov 11 08:34:54 CET 2013


On Sun, 10 Nov 2013 20:13:45 -0800, Rick Johnson wrote:

>   What good is ANY namespace when you cannot override it's fundamental
>   interface? And interfaces are the key to OOP!
> 
>   Is __setattr__/__getattr__ ringing a bell people?


import sys
sys.modules["mymodule"] = any_object_you_like()


Want a module with __setattr__ and __getattr__? Make a class, instantiate 
it, and shove it in sys.modules.


This has worked since Python 2.1:

http://code.activestate.com/recipes/65207-constants-in-python/



-- 
Steven



More information about the Python-list mailing list