Skip to content

Instantly share code, notes, and snippets.

@zhuifengshen
Created November 15, 2018 08:50
Show Gist options
  • Save zhuifengshen/b5fcf8441ffd97186cd1b740f9fa3eb3 to your computer and use it in GitHub Desktop.
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.
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