Created
November 15, 2018 18:37
-
-
Save benjsto/a72bbef0915031a94490bcf404b678bb to your computer and use it in GitHub Desktop.
python decorators
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 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