Created
January 9, 2018 18:57
-
-
Save shelling/7d9fb3c4c1fb516252178b527a99acfe to your computer and use it in GitHub Desktop.
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
class Hooks(object): | |
@staticmethod | |
def before(name): | |
def decorator(func): | |
def wrap(self, *args): | |
self.__getattribute__(name)() | |
func(self, *args) | |
return wrap | |
return decorator | |
@staticmethod | |
def after(name): | |
def decorator(func): | |
def wrap(self, *args): | |
func(self, *args) | |
self.__getattribute__(name)() | |
return wrap | |
return decorator | |
class Hello(Hooks): | |
def do_something(self): | |
print("do something before") | |
@Hooks.before("do_something") | |
@Hooks.after("burn") | |
def run(self): | |
print("real run") | |
def bar(self): | |
print("bar") | |
def burn(self): | |
print("burn") | |
Hello().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment