I have put decorators in quotes as these aren't strict decorators by python standards although they need to act like one.
Let's say I have 3 decorators, f,g,h.
def f(x):
return x+1
def g(x):
return x+2
def h(x):
return x * 3
and my "real" fn
def orig_fn(z):
return z + 100
How can I modify orig_fn so that I can chain at runtime different combinations of f,g,h ?
If I use more than one of f,g,h then they should all be applied - ie orig_fn may return f(g(orig_fn(x)) .
I've tried something like (the real thing I'm doing is modifying a class function - hence the inclusion of MethodType). fn_chain would be something like [f,g] and I'm trying to solve this generically - current_fn is the original function found in the class.
if fn_chain:
print "-> creating chain"
for fn in fn_chain:
def modified_fn(self,*args,**kwargs):
return fn(self, current_fn, *args,**kwargs)
#current_fn = modified_fn
setattr(cls,f_name,types.MethodType(modified_fn,cls))
orig_fnso that it lets you specify other functions to chain? What kind of API are you looking for here?orig_fncall from happening at all.