Merge decorator function as class

Posted by SyetemHog on Stack Overflow See other posts from Stack Overflow or by SyetemHog
Published on 2010-05-02T17:51:27Z Indexed on 2010/05/02 17:58 UTC
Read the original article Hit count: 194

Filed under:
|
|
|
|

How to make this merge function as class decorator?

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

© Stack Overflow or respective owner

Related posts about python

Related posts about decorator