Last active
May 29, 2019 09:11
-
-
Save tristan2077/f212a7fbac4fd6c2a13d0983d4634d19 to your computer and use it in GitHub Desktop.
函数执行前后hook
This file contains 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
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