Help in using introspection to simplify repetitive code
Tim N. van der Leeuw
tim.leeuwvander at nl.unisys.com
Mon Aug 21 08:55:34 EDT 2006
Fredrik Lundh wrote:
> jsceballos at gmail.com wrote:
>
> > 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>
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
More information about the Python-list
mailing list