Tponthieux, what you can do is setting the attribute of the called function (method) and then retrieving it: Simple example ( updated ): def validate(func): def wrapped(self, *args, **kwargs): self. Valid = True func(self, *args, **kwargs) return wrapped class TestClass(object): @validate def do_some_work(self): print "some work done" tc = TestClass() tc. Do_some_work() print tc.valid.
Tponthieux, what you can do is setting the attribute of the called function (method) and then retrieving it: Simple example (updated): def validate(func): def wrapped(self, *args, **kwargs): self. Valid = True func(self, *args, **kwargs) return wrapped class TestClass(object): @validate def do_some_work(self): print "some work done" tc = TestClass() tc. Do_some_work() print tc.valid.
This looks promising but I'm not sure how to let the decorator code access the getvalue() method of the base class. Does anyone want to try to work this technique into the question example? – tponthieux May 30 at 16:54 Allright, another example :) But I think this one will definitely help you.
– BasicWolf May 30 at 17:07 You never call the wrapped func, you only return it from the wrapper. That's not going to give you the expected results ;-) – Martijn Pieters May 30 at 17:20 Updated. Now do_some_work() is called :P – BasicWolf May 30 at 17:31 You are getting confused about *args and **kwargs there, I think.
You'll need to pass them to do_some_work with those attributes. – Martijn Pieters May 30 at 17:37.
You can pass the return value of the validate method along if you like: class BaseClass(object): def getvalue(self): return True def validate(input_function): def wrapper(self, *args, **kwargs): self. Validated = self.getvalue() if not self. Validated: print "Not validated.
" return input_function(self, validated=self. Validated, *args, **kwargs) return wrapper validate = staticmethod(validate) class ExtendedClass1(BaseClass): @BaseClass. Validate def do_some_work(self, validated=None): print "Things are validated if the method got this far.
", validated class ExtendedClass2(BaseClass): @BaseClass. Validate def do_some_work(self, validated=None): print "Things are validated if the method got this far.", validated class ExtendedClass3(BaseClass): def do_some_work(self): print "This one doesn't require validation. " work1 = ExtendedClass1() work1.
Do_some_work() work2 = ExtendedClass2() work2. Do_some_work() work3 = ExtendedClass3() work3. Do_some_work() The key here is adding self to the wrapper function.
What happens is that your decorated functions do not get bound to the instance (and become methods), but the function returned by the decorator (wrapper in the above example) get's bound instead.So this function will get the self (the instance) parameter passed in when called! It's important to remember that what a @decorator does is simply call decorator passing in the function you are decorating, and then replace the function you are decorating with whatever the decorator returned. In your example this is wrapper, and to the class, there is no difference between that and the original function before decorating.In the above example, I declared self explicitly.
If we hadn't, we could also have just taken it from args: def validate(input_function): def wrapper(*args, **kwargs): print "args0 is now the instance (conventionally called 'self')", args0 self = args0 self. Validated = self.getvalue() if not self. Validated: print "Not validated.
" return input_function(validated=self. Validated, *args, **kwargs) Also note that we pass an extra keyword argument to the wrapped method, named validated. This is entirely optional, you can just drop the validated=self.
Validated and validated=None parts from the example.
– Martijn Pieters May 30 at 18:04 I wasn't the downvoter, but the negative vote may be because the answer doesn't run as posted. One thing to note is that @BaseClass. Validate has to call a method described in the base class outside of the decorator method.
Validated = self.getvalue() I'll edit the first code block of the question to make that a little more clear. – tponthieux May 30 at 19:31 Martijn, I apologize for downvoting with no reason. There is absolutely nothing wrong with your answer, in fact I also learned from it.
Could you please edit it just a bit, so I could cancel that "-1". Thank you. – BasicWolf May 30 at 19:35 Ah, I first read this as "However, I need to call a method of the base class in the decorator to do the validation work, and retrieve the value of (validated) in the child class.
" but later on you want to set self.validated. I'll update. – Martijn Pieters May 30 at 20:05 @tponthieux: updated to include the def getvalue(self) method from the OP, which code adjusted to call that instead of the fictional def validate(self) method.
The code example executes as is now. – Martijn Pieters May 30 at 20:26.
Except, in the above, wrappedClass is not a class but a function manipulated to return a class type. How would this be done?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.