Ulrich Eckhardt wrote: > Hi! > > I have an extension module (a plugin written with Boost.Python) and around > that a wrapper class that adapts a few things. Since the module is a > plugin, there are multiple implementations of this. What I'm currently > doing is this: > > plugin = __import__(plugin_name) > > class PluginWrapper(plugin.PluginClass): > ... > > This means that the definition of class PluginWrapper actually depends on > the previously loaded module. What I would like to do is to define the > wrapper just once and instead pass the plugin module to the constructor: > > class PluginWrapper(object): > ... > plugin = __import__(plugin_name) > instance = PluginWrapper(plugin) > > > Now, I use the wrapper to make some function more friendly (e.g. default > parameters, keyword-parameters, wrapping raw handles) but I want other > functions from the baseclass to remain untouched. If I use a baseclass, > this lookup is automatic. However, when I pass the instance to the > constructor, I have to store it in a member, and then I have to add code > for every function only to delegate it to that member. > > Is there an easy and generic way out of this?
Use getattr() >>> class W(object): ... def __init__(self, wrapped): self._wrapped = wrapped ... def __getattr__(self, name): ... return getattr(self._wrapped, name) ... >>> class A(object): ... def hello(self): print "hello" ... >>> a = A() >>> w = W(a) >>> w.hello() hello However, with newsytle classes this doesn't work for __special__ methods >>> w.__str__() '<__main__.W object at 0x7f04ef2d4c50>' Peter -- http://mail.python.org/mailman/listinfo/python-list