is decorator the right thing to use?



Hi,

after hearing a lot about decorators and never actually using one I have
decided to give it a try. My particular usecase is that I have class that
acts as a proxy to other classes (i.e. passes messages along to those
classes) however hand-coding this type of class is rather tedious, so I
decided to use decorator for that. Can somebody tell me if what I'm doing
is a potential shot-in-the-foot or am I on the right track? (Note, It's
rather rudimentary proof-of-concept implementation and not the final
solution I'm about to employ so there are no optimizations or
signature-preserving code there yet, just the idea).

Here's the code:

class A:
b=None
def __init__(self,b):
self.val='aval'
self.b=b
b.val='aval'

def mymethod(self,a):
print "A::mymethod, ",a

def mymethod2(self,a):
print "A::another method, ",a


def Aproxy(fn):
def delegate(*args,**kw):
print "%s::%s" % (args[0].__class__.__name__,fn.__name__)
args=list(args)
b=getattr(args[0],'b')
fnew=getattr(b,fn.__name__)
# get rid of original object reference
del args[0]
fnew(*args,**kw)
setattr(A,fn.__name__,delegate)
return fn

class B:
def __init__(self):
self.val='bval'

@Aproxy
def bmethod(self,a):
print "B::bmethod"
print a, self.val

@Aproxy
def bmethod2(self,a):
print "B::bmethod2"
print a, self.val

b=B()
b.bmethod('foo')
a=A(b)
b=B()
b.val='newval'
a.bmethod('bar')
a.bmethod2('zam')


.



Relevant Pages

  • Re: remaining decorator syntax options
    ... >The idea being that decorators and other function properties should ... >chance of swaying GvR on this count. ... >def f: ... However with a keyword ...
    (comp.lang.python)
  • Re: tweaking @decorator syntax
    ... >two things struck me about the location of the decorators ... > def classmethod1: ... Obviously you could define metafunctions to apply default decorators, ... Of course, instantiation of an exception is just a special case of instantiating a class, ...
    (comp.lang.python)
  • Re: Decorator Base Class: Needs improvement.
    ... See Raymond Hettinger's optimizing decorators ... that can't be done with a wrapper? ... class wrapper: def __call__: # preprocess x ... I can understand it if you are merely pursuing this topic because of your fascination with the capabilities of Python, but I don't have the feeling that there are legion Python programmers out there waiting impatiently to be able to build wrapped functions. ...
    (comp.lang.python)
  • Re: PEP 3107 Function Annotations for review and comment
    ... here's a potentially nifty way of adding decorators to input args for python: ... def a, arg2, tuple): ... any type conversion method should throw ValueError on failure (this would allow python to catch this error and throw a new exception (InputArgError) or something ... self-evident what purpose function annotations would serve. ...
    (comp.lang.python)
  • Re: @decorator syntax is sugar, but for what exactly?
    ... > decorators do not do a complete job after all. ... act like functions, while properties act like variables; ... I, personally, don't like the idea of overloading def in this way. ... generalizing iteration via generators, and I see utility in generalizing ...
    (comp.lang.python)