Skip to content

Instantly share code, notes, and snippets.

@hartym
Created July 7, 2010 10:30
Show Gist options
  • Select an option

  • Save hartym/466536 to your computer and use it in GitHub Desktop.

Select an option

Save hartym/466536 to your computer and use it in GitHub Desktop.
# 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