Below for you to share a Python decorator based on the method examples in the class, has a good reference value, I hope to be helpful to everyone. Come and see it together.
Title:python Decorative Decoration class method
Comments:true
DATE:2017-04-17 20:44:31
Tags: [' Python ', ' decorate ']
Category: [' Python ']
---
Most of the tutorials currently available on the Chinese web for decorators are about decorating a common function. This article describes how to use the Python adorner to decorate a class method, while invoking other methods within the class in the adorner function. This article describes an exception that captures a method as an example.
There is a class test, which has the following structure:
Class Test (object): Def __init__ (self): pass def revive (self): print (' Revive from exception. ') # do something to restore Def read_value (self): print (' Here I'll do something. ') # do something.
There is a method Read_value () in the class, and this method is called in many places. For some reason, the method read_value may randomly throw exception causing the program to crash. So need to do the whole method try ... except processing. The ugliest thing to do is as shown in the following code:
Class Test (object): Def __init__ (self): pass def revive (self): print (' Revive from exception. ') # do something to restore Def read_value (self): try: print (' Here I'll do something. ') # do something. Except Exception as E: print (f ' Exception {e} raised, parse Exception. ') # do other thing. Self.revive ()
This can solve the problem, but the code is not pythonic.
Using adorners to solve this problem, should the adorner function be written inside the class or outside the class? The answer is, write outside the class. So how do you invoke other methods of this class, since it's written outside of the class?
First, write an adorner that handles the most common exceptions:
def catch_exception (Origin_func): Def wrapper (*args, **kwargs): try: u = Origin_func (*args, **kwargs) return u except Exception: Return ' an Exception raised. ' Return to Wrapperclass Test (object): Def __init__ (self): C5/>pass def revive (self): print (' Revive from exception. ') # do something to restore @catch_exception def read_value (self): print (' Here I'll do something. ') # do something.
This way of writing can actually catch the exception of Origin_func (), but what if, in the event of an exception, you need to invoke another method inside the class to handle the exception? The answer is to add a parameter to wrapper: self.
The code changes to the following form:
def catch_exception (Origin_func): Def wrapper (self, *args, **kwargs): try: u = origin_func (self, *args, * * Kwargs) return u except Exception: self.revive () #不用顾虑, call the original class directly method return ' an Exception raised. ' Return Wrapperclass Test (object): Def __init__ (self): pass def revive (self): print (' Revive from exception. ') # do something to restore @catch_exception def read_value (self): print (' Here I'll do something. ') # do something.
You only need to modify the part of the adorner definition, where the adorner is used without modification at all.
Running results for normal run time:
Catch and handle exceptions after an exception occurs:
By adding a self parameter, the adorner outside the class can directly use the various methods within the class, or you can use the properties of the class directly.