Last active
April 1, 2019 06:02
-
-
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.
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
#!/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