Created
April 17, 2017 07:42
-
-
Save kingname/8fdbea04d072408603386dcb3d17f738 to your computer and use it in GitHub Desktop.
[Python decorate use class method] tags:Python, decorate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def catch_exception(origin_func): | |
def wrapper(self=None, *args, **kwargs): | |
print('start') | |
try: | |
u = origin_func(self, *args, **kwargs) | |
return u | |
except Exception: | |
self.z() | |
return 'an Exception raised.' | |
return wrapper | |
class X(object): | |
def __init__(self): | |
pass | |
@catch_exception | |
def y(self, a, b): | |
print('the parameter are: {}, {}'.format(a, b)) | |
return 'I am Y, a method of X.' | |
def z(self): | |
print('I am Z, a method of X, I am here because Y died.') | |
v = X() | |
print(v.y(1)) #here will return False because the number of parameter is not enougth | |
print(v.y(1, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment