简体   繁体   中英

Get passing argument at python decorator

def deco_test(func):
    def inner_function(*args, **kwargs):
        return func(*args, **kwargs)
    return inner_function

@deco_test
def a(a='asdf', b='asdf'):
    pass

if __name__ == '__main__':
    a(456, 789)
    a(123, b='qwer/123/a.txt')
    a(a='098', b='789456')    

I want to get argument like this:

  1. {'a': 456, 'b': 789}
  2. {'a': 123, 'b': 'qwer/123/a.txt'}
  3. {'a': '098', 'b':'789456'}

in deco_test(func)

You will not get access to the parameter you provide to a inside deco_test . The decoration is done when the function is declared, before you call the function and at that point in time you have no information about those arguments.

Inside the inner_function you get arguments as provided in the decorated function call a(456, 789) will give you args=(456, 789) . If you want to format them according to the arguments that a can take, I think you have to inspect on the function a .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM