簡體   English   中英

在python裝飾器處獲取傳遞參數

[英]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')    

我想得到這樣的論點:

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

deco_test(func)

你將不能訪問你提供的參數a內部deco_test 修飾是在聲明函數時完成的,在調用函數之前,那時您沒有關於這些參數的信息。

inner_function內部,您得到修飾函數調用a(456, 789) inner_function中提供的參數a(456, 789)將為您args=(456, 789) 如果要根據a可以接受的參數來格式化它們,我認為您必須檢查函數a

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM