delegate functions to member

U

Ulrich Eckhardt

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?

Thanks!

Uli
 
P

Peter Otten

Ulrich said:
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()
.... def __init__(self, wrapped): self._wrapped = wrapped
.... def __getattr__(self, name):
.... return getattr(self._wrapped, name)
........ def hello(self): print "hello"
....hello

However, with newsytle classes this doesn't work for __special__ methods
'<__main__.W object at 0x7f04ef2d4c50>'

Peter
 
C

Chris Rebert

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?

Create the subclass(es) dynamically:

def wrap(plug_in):
class PluginWrapper(plug_in.PluginClass):
...
return PluginWrapper

plugin = __import__(plugin_name)
WrappedPlugin = wrap(plugin)

Cheers,
Chris
 
U

Ulrich Eckhardt

Peter said:
Use getattr()

... def __init__(self, wrapped): self._wrapped = wrapped
... def __getattr__(self, name):
... return getattr(self._wrapped, name)
...

I thought there was something like this, thanks! :)

When I read this, I thought "OK, now I only have check first if the
attribute can be looked up in 'self' first", but even that isn't the case.
I tried it and added another function to class W above, which I can call
just as if it was defined in _wrapped, so obviously (?) the __getattr__
lookup isn't done there.

So, short follow-up question: Why does this work?

Uli
 
P

Peter Otten

Ulrich said:
I thought there was something like this, thanks! :)

When I read this, I thought "OK, now I only have check first if the
attribute can be looked up in 'self' first", but even that isn't the case.

The getattr() call inside __getattr__() raises an AttributeError if it can't
find an attribute called name in self._wrapped. This very thing you'd have
to do if you wanted to signal that an attribute doesn't exist, like in
.... def __getattr__(self, name):
.... if name == "foo": return 42
.... raise AttributeError
....Traceback (most recent call last):
File "<stdin>", line 1, in <module>
I tried it and added another function to class W above, which I can call
just as if it was defined in _wrapped, so obviously (?) the __getattr__
lookup isn't done there.

So, short follow-up question: Why does this work?

__getattr__() is a fallback that is only tried when the normal lookup fails.
If you need to intercept every attribute lookup use __getattribute__()
instead:
.... def __getattr__(self, name):
.... print "__getattr__(%r)" % name
.... return 42
.... def __getattribute__(self, name):
.... print "__getattribute__(%r)" % name
.... return super(A, self).__getattribute__(name)
....
__getattribute__('foo')
'yadda'
__getattribute__('bar')
__getattr__('bar')
42

Peter
 
U

Ulrich Eckhardt

Peter said:
__getattr__() is a fallback that is only tried when the normal lookup
fails. If you need to intercept every attribute lookup use
__getattribute__() instead:

Thank you Peter, that was the missing piece to understand it!

Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top