Skip to content

Instantly share code, notes, and snippets.

@benjsto
Created November 15, 2018 18:37
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 benjsto/a72bbef0915031a94490bcf404b678bb to your computer and use it in GitHub Desktop.
Save benjsto/a72bbef0915031a94490bcf404b678bb to your computer and use it in GitHub Desktop.
python decorators
def once(f):
def fn(*args, **kwargs):
if not fn.ran:
fn.ran = True
return f(*args, **kwargs)
print("Not calling again.")
fn.ran = False
return fn
@once
def decorated_method(message):
print(message)
def not_decorated_method(message):
print(message)
if __name__ == '__main__':
for msg in ["Message 1!", "Message 2!", "Message 3!"]:
decorated_method(msg)
for msg in ["Message 1!", "Message 2!", "Message 3!"]:
not_decorated_method(msg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment