Created
July 7, 2010 10:30
-
-
Save hartym/466536 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
| # Decorate manually with this. | |
| def fixparams(decorated, **fixed): | |
| def _fixparams(*args, **kwargs): | |
| kwargs.update(fixed) | |
| return decorated(*args, **kwargs) | |
| return _fixparams | |
| # old school / inline syntax | |
| def myfunc(a, b, c): | |
| print '%s %s %s' % (a, b, c) | |
| fixbc = fixparams(myfunc, b='fixedbee', c='fixedsea') | |
| fixbc('hey!') | |
| fixbc('hey again', b='originalb') | |
| print '\n-----------------------------------\n' | |
| # one decoration level more is needed if you want to parameterize the | |
| # decorator. | |
| def fixparams2(**fixed): | |
| def _fixparams2(decorated, *args, **kwargs): | |
| return fixparams(decorated, **fixed) | |
| return _fixparams2 | |
| # decorator syntax | |
| @fixparams2(b='newbee', c='seethis') | |
| def fixbcagain(a, b, c): | |
| print '%s %s %s (again)' % (a, b, c) | |
| # and call it | |
| fixbcagain('ohohoh') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment