Skip to content

Instantly share code, notes, and snippets.

@kingname
Created April 17, 2017 07:42
Show Gist options
  • Save kingname/8fdbea04d072408603386dcb3d17f738 to your computer and use it in GitHub Desktop.
Save kingname/8fdbea04d072408603386dcb3d17f738 to your computer and use it in GitHub Desktop.
[Python decorate use class method] tags:Python, decorate
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