Help in using introspection to simplify repetitive code

J

jsceballos

Hello.
I'm writing a proxy class, i.e: a class whose methods mostly delegate
their functionality to other class object. Most of the methods (which
are quite a lot) defined in the class would end up being:

def thisIsTheMethodName(self):
self._handlerClass.thisIsTheMethodName()

The handler object is the same in all methods.

I was wondering if there is a way to simplify this proxy class, maybe
using some pythonic technique like metaclasses, introspection... any
suggestion is appreciated.

Thanks,

Javier Sanz
 
S

Simon Percivall

Hello.
I'm writing a proxy class, i.e: a class whose methods mostly delegate
their functionality to other class object. Most of the methods (which
are quite a lot) defined in the class would end up being:

def thisIsTheMethodName(self):
self._handlerClass.thisIsTheMethodName()

The handler object is the same in all methods.

I was wondering if there is a way to simplify this proxy class, maybe
using some pythonic technique like metaclasses, introspection... any
suggestion is appreciated.

Thanks,

Javier Sanz

http://docs.python.org/ref/attribute-access.html#l2h-206
 
T

Terry Reedy

Hello.
I'm writing a proxy class, i.e: a class whose methods mostly delegate
their functionality to other class object. Most of the methods (which
are quite a lot) defined in the class would end up being:

def thisIsTheMethodName(self):
self._handlerClass.thisIsTheMethodName()

Are these parameterless static methods
or should this be self._handlerClass.thisIsTheMethodName(self)
or does self get auto-bound even though not a _handlerClass instance?
(I have never needed or done such delegation.)
The handler object is the same in all methods.

I was wondering if there is a way to simplify this proxy class, maybe
using some pythonic technique like metaclasses, introspection... any
suggestion is appreciated.

My immediate thought would be to start with

_forwarded = set(......) # of forwarded method names
def __getattr__(self, name):
if name in _forwarded: return getattr(self._handlerClass, name)

but I am not sure if this gives the right wrapping and binding.

Terry Jan Reedy
 
J

jsceballos

As you mention, wether the methods take arguments or not is something
to have into account.
And they do take arguments, and a variable number of them, so AFAIK
hooking with __getattr__ or __getattribute__ will not work, as you can
only get the method name with that. I was thinking of some __call__
overriding, but I've never done it before and I don't know if this
could be the way to go.
--


Un saludo,

Javier
 
F

Fredrik Lundh

And they do take arguments, and a variable number of them, so AFAIK
hooking with __getattr__ or __getattribute__ will not work, as you can
only get the method name with that.

why not just return the bound method *object* (a callable), and let the
caller call that as usual (see Terry's last example).

(hint: x.foo() can be written f=getattr(x,"foo"); f())


</F>
 
T

Tim N. van der Leeuw

Fredrik said:
why not just return the bound method *object* (a callable), and let the
caller call that as usual (see Terry's last example).

(hint: x.foo() can be written f=getattr(x,"foo"); f())


</F>

I can tell you from my experience that this works; I've used this
before to make something very much like this proxy-class:

class RequestCalculations(object):
def __init__(self, request):
self.serviceType, self.facade =
makeMessageFacadeInstance(request)
return

def __getattr__(self, name):
return getattr(self.facade, name)

(rest of the code omitted)

Cheers,

--Tim
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
As you mention, wether the methods take arguments or not is something
to have into account.
And they do take arguments, and a variable number of them, so AFAIK
hooking with __getattr__ or __getattribute__ will not work, as you can
only get the method name with that.

Nope. Defining __getattr__ is the canonical pythonic way to do
delegation. Remember that in Python, functions and methods are objects
too and can be passed around/returned etc just like any other object.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top