I'm not sure I quite get what you're asking. Your implementation works fine, and you won't get around having two levels of indirection if you want to create a parametrized decorator of any kind.
To make merge a class you could do this
class Merge(object):
def __init__(self, **extra_kws):
self.extra_kws = extra_kws
def __call__(self, function):
def _wrapper(*args, **kws):
kws.update(self.extra_kws)
return function(*args, **kws)
return _wrapper
Then you can do this:
@Merge(foo='bar')
def test(*args, **kws):
print *args
print **kws
But you said you want to add change and process new arguments. So presumably you want the decorator itself to be live so you can do:
test.extra_kws['sun'] = 'dock'
After the decorator has been applied. In that case you probably don't want merge to be a class, but you want it to generate a class, so that test
is replaced by the modifiable instance:
def merge(**extra_kws):
class _Merge(object):
def __init__(self, function):
self.extra_kws = extra_kws
self.function = function
def __call__(self, *args, **kws):
kws.update(self.extra_kws)
return self.function(*args, **kws)
return _Merge
@merge(foo='bar')
def test(*args, **kws):
print 'args:', args
print 'kws:', kws
test(sun='dock')
test.extra_kws['trog'] = 'cube'
test(sun='dock')
This then allows you to change the keywords on a particular decorated function later.
You could also do the same thing with function arguments without classes:
def merge(**extra_kws):
def _decorator(function):
def _wrapper(*args, **kws):
kws.update(_wrapper.extra_kws)
return function(*args, **kws)
_wrapper.extra_kws = extra_kws
return _wrapper
return _decorator
@merge(foo='bar')
def test(*args, **kws):
print 'kws:', kws
test(sun='dock')
test.extra_kws['trog'] = 'cube'
test(sun='dock')