Help in using introspection to simplify repetitive code

Discussion in 'Python' started by jsceballos@gmail.com, Aug 20, 2006.

  1. Guest

    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
     
    , Aug 20, 2006
    #1
    1. Advertising

  2. wrote:
    > 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
     
    Simon Percivall, Aug 20, 2006
    #2
    1. Advertising

  3. Terry Reedy Guest

    <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Aug 20, 2006
    #3
  4. Guest

    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
     
    , Aug 21, 2006
    #4
  5. 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>
     
    Fredrik Lundh, Aug 21, 2006
    #5
  6. Fredrik Lundh wrote:
    > 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
     
    Tim N. van der Leeuw, Aug 21, 2006
    #6
  7. 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.
     
    Bruno Desthuilliers, Aug 27, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry

    Re: Repetitive code (Newbie)

    Jerry, Aug 6, 2003, in forum: VHDL
    Replies:
    2
    Views:
    1,057
    Mike Treseler
    Aug 7, 2003
  2. smu
    Replies:
    0
    Views:
    947
  3. ben
    Replies:
    8
    Views:
    394
  4. James Yong

    Can I simplify this code?

    James Yong, Dec 6, 2006, in forum: Java
    Replies:
    6
    Views:
    322
    Tris Orendorff
    Dec 15, 2006
  5. MC felon
    Replies:
    4
    Views:
    346
    MC felon
    Dec 10, 2006
Loading...

Share This Page