Simon Burton simon at
Wed Aug 10 05:50:26 CEST 2005

I've been needing a module level __getattr__ for some c library
wrapping. This has solved the problem:

import sys
from new import module
class ModuleProxy(module):
  def __init__( self, name, master ):
    module.__init__( self, name )
    self._master = master
    self.__dict__["__all__"] = dir(master)
  def __getattr__(self, name):
    attr = getattr( self._master, name )
    return attr

# ... end of file:
sys.modules["mymod"] = ModuleProxy("mymod",sys.modules["mymod"])

--Simon Burton

More information about the Python-list mailing list