Skip to content

Instantly share code, notes, and snippets.

@tristan2077
Last active May 29, 2019 09:11
Show Gist options
  • Save tristan2077/f212a7fbac4fd6c2a13d0983d4634d19 to your computer and use it in GitHub Desktop.
Save tristan2077/f212a7fbac4fd6c2a13d0983d4634d19 to your computer and use it in GitHub Desktop.
函数执行前后hook
from functools import wraps
def _exec_hook(hook_name, self):
if hasattr(self, hook_name):
getattr(self, hook_name)()
def hooks(fn):
@wraps(fn)
def hooked(self):
fn_name = getattr(fn, 'func_name', fn.__name__)
_exec_hook('pre_' + fn_name, self)
val = fn(self)
_exec_hook('post_' + fn_name, self)
return val
return hooked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment