<font color="#000000">Hello,</font><div><font color="#000000"><br></font></div><div><font color="#000000">I was having some trouble understanding decorators and inheritance and all that. This is what I was trying to do:</font></div>
<div><font color="#000000"><br></font></div><div><font color="#000000"># untested</font></div><div><font color="#000000">class A(object):</font></div><div><font color="#000000"> def _protector_decorator(fcn):</font></div>
<div><font color="#000000"> def newfcn(self, *args, **kwargs):</font></div><div><font color="#000000"> return fcn(self, *args, **kwargs)</font></div><div><font color="#000000"> return newfcn</font></div>
<div><font color="#000000"><br></font></div><div><font color="#000000"> @_protector_decorator</font></div><div><font color="#000000"> def my_method(self, *args, **kwargs):</font></div><div><font color="#000000"> """ do something here """</font></div>
<div><font color="#000000"><br></font></div><div><font color="#000000">class B(A):</font></div><div><font color="#000000"> def _protector_decorator(fcn):</font></div><div><font color="#000000"> def newfcn(self, *args, **kwargs):</font></div>
<div><font color="#000000"> raise MyException('I do not want B to be able to access the protected functions')</font></div><div><font color="#000000"> return newfcn</font></div><div><font color="#000000"><br>
</font></div><div>The goal of all that was to be able to change the behavior of my_method inside class B simply by redefining the decorator. Basically, what I want is B.my_method() to be decorated by B._protector_decorator, but in the code I'm running it's decorated by A._protector_decorator.</div>
<div><br></div><div>I presume this is because once the decorator is applied to my_method in class A, A.my_method is immediately bound to the new, 'decorated' function, which is subsequently inherited (and not decorated, obviously), by B.</div>
<div><br></div><div>Am I correct here? My workaround was to simply copy the method from class A to class B, after which B._protector_decorator decorated the methods in B. While this doesn't make the use of decorators completely pointless (the decorators actually do something in each class, it's just different), it does add a bunch of code duplication which I was at one point hopeful to avoid.</div>
<div><br></div><div>I'm still stumbling around with decorators a little, but this exercise has made them a lot clearer to me.</div><div><br></div><div>Thanks!</div><div>Jason</div><div><br></div>