Skip to content

Instantly share code, notes, and snippets.

@shelling
Created January 9, 2018 18:57
Show Gist options
  • Save shelling/7d9fb3c4c1fb516252178b527a99acfe to your computer and use it in GitHub Desktop.
Save shelling/7d9fb3c4c1fb516252178b527a99acfe to your computer and use it in GitHub Desktop.
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