Skip to content

Instantly share code, notes, and snippets.

@xiaket
Last active April 1, 2019 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaket/3853a44dc02b127b5f8cac762f889b1a to your computer and use it in GitHub Desktop.
Save xiaket/3853a44dc02b127b5f8cac762f889b1a to your computer and use it in GitHub Desktop.
Define a class method in Python that can work as a decorator for other methods.
#!/usr/bin/env python3
class A:
def dec(func):
def wrapper(self, *args, **kwargs):
print("Pre")
func(self, *args, **kwargs)
print("Post")
return wrapper
@dec
def hello(self, name="world"):
print(f"Hello {name}")
a = A()
a.hello()
a.hello("rabbit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment