Created
November 15, 2018 08:50
-
-
Save zhuifengshen/b5fcf8441ffd97186cd1b740f9fa3eb3 to your computer and use it in GitHub Desktop.
Decorate method with this to check whether the object has an attribute with the given name.
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
def check(attr): | |
def decorator(method): | |
""" | |
Decorate method with this to check whether the object has an attribute with the given name. | |
""" | |
@wraps(method) | |
def wrapper(self, *args, **kwargs): | |
if hasattr(self, attr): | |
return method(self, *args, **kwargs) | |
return None | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment