Help in using introspection to simplify repetitive code
Terry Reedy
tjreedy at udel.edu
Sun Aug 20 16:59:23 EDT 2006
<jsceballos at gmail.com> wrote in message
news:1156090845.875872.41220 at 75g2000cwc.googlegroups.com...
> 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
More information about the Python-list
mailing list