Created
November 1, 2017 18:05
-
-
Save dansondergaard/69e9b5d579cd6c572e89b95cf9729647 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| """Example of adding attributes to a function using a decorator.""" | |
| import time | |
| def delayable(func): | |
| def _delayer(delay, *args, **kwargs): | |
| time.sleep(delay) | |
| return func(*args, **kwargs) | |
| setattr(func, 'delay', _delayer) | |
| return func | |
| @delayable | |
| def say_hello(name): | |
| print('Hello', name) | |
| say_hello('Anne') | |
| say_hello.delay(5, 'Dan') | |
| say_hello.delay(10, 'Torben') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment