Properties for modules?

Ed Leafe ed at leafe.com
Mon Jun 11 20:40:32 EDT 2007


On Jun 11, 2007, at 5:55 PM, Steven Bethard wrote:

> I typically define a module wrapping class like::
>
>      class GiveThisModuleProperties(object):
>          def __init__(self, module_name):
>              self._module = sys.modules[module_name]
>              sys.modules[module_name] = self
>          # now define whatever behavior you need
>          def __getattr__(...):
>              ...
>          def __setattr__(...):
>              ...
>
> Then, in the module you want wrapped, you write::
>
>      GiveThisModuleProperties(__name__)
>
> The trick here is basically that we replace the module object in
> sys.modules with a class instance that wraps the module with whatever
> extra behavior is necessary.

	OK, I see the trick involved. Yes, that does work for what I need.  
Thanks!

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com





More information about the Python-list mailing list