I want to decorate a class method, but can't quite figure out how to do it. Minimal class example:
class Circle(object): def __init__(self,radius): self.R = radius def arc_length(self,theta): return self.R*theta Now let's say I want another method which takes the arc_length always relative to some particular other value of arc length so I define a function as follows:
def relative(rel_val): def decorator(fn): def wrapper(*args): return fn(*args) - fn(rel_val) return wrapper return decorator so now I extend my class to:
class Circle(object): def __init__(self,radius): self.R = radius def arc_length(self,theta): return self.R*theta @relative(0.2) def relative_arc_length(self,theta): return self.arc_length(theta) I would hope this would give the desired behaviour, but I get error: relative_arc_length() takes exactly 2 arguments (1 given)
I assume this is something to do with the self argument, but if I add self to the fn(self,rel_val) in the relative definition it gives me: global name "self" is not defined
So how do I fix this? Interestingly, if I do the following:
c = Circle(10) relative(0.2)(c.arc_length)(1.5) it works as expected.
Can anyone point me to where I'm going wrong? Also, is it possible to define the decorated function inside the class, and if so, should I do this? Is it good programming practice?