views:

61

answers:

1

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
A: 

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')
Ian