Need to create a class that will do all things as the “merge” function. In class i will change, process and add new arguments.
def merge(*arg, **kwarg): # get decorator args & kwargs
def func(f):
def tmp(*args, **kwargs): # get function args & kwargs
kwargs.update(kwarg) # merge two dictionaries
return f(*args, **kwargs) # return merged data
return tmp
return func
Usage:
@other_decorator # return *args and **kwarg
@merge(list=['one','two','three']) # need to merge with @other_decorator
def test(*a, **k): # get merged args and kwargs
print 'args:', a
print 'kwargs:', k
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
Then you can do this:
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:
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
testis replaced by the modifiable instance: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: