Created
March 2, 2016 08:27
-
-
Save davidbj/3c74b8d7954c409c316a to your computer and use it in GitHub Desktop.
A decorator demo
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
| def inject_user(default_user=None): | |
| def user(fn): | |
| def wraps(*args, **kwargs): | |
| if kwargs.get('user'): | |
| return kwargs['user'] | |
| else: | |
| return default_user | |
| return wraps | |
| return user | |
| @inject_user(default_user="David") | |
| def do_something(*args, **kwargs): | |
| return kwargs | |
| do_something(user='David.zhang') | |
| do_something() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment