2 回答

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
裝飾器語法只是函數(shù)應(yīng)用的快捷方式; 只是一個(gè)函數(shù)。創(chuàng)建一個(gè)使用固定參數(shù)調(diào)用它的新函數(shù)。app.callback
def new_callback(plot):
return app.callback(
Output(plot, "figure"),
[
Input("checklist", "value"),
Input("radio", "value")
]
)
app.callback本身實(shí)際上不是裝飾者;它是一個(gè)返回裝飾器的函數(shù)(即,將要修飾的函數(shù)作為參數(shù)的函數(shù))。
@app.callback(...)
def foo():
...
等效于
def foo():
...
foo = app.callback(...)(foo)

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以將裝飾器包裝在其自己的裝飾器中,并使用您在任何子函數(shù)中傳遞的參數(shù),如下所示:
def decorator_wrapper(arg1):
# do something with arg1:
print('Inside decorator_wrapper, arg1: ' + str(arg1))
def decorator(function):
print('Inside decorator, arg1: ' + str(arg1))
def wrapper(*args, **kwargs):
print('Inside wrapper, arg1: ' + str(arg1))
# execute decorated function:
result = function(*args, **kwargs)
return result
return wrapper
return decorator
@decorator_wrapper('a')
def MyFunction():
print('This is my function')
MyFunction()
這將導(dǎo)致:
Inside decorator_wrapper, arg1: a
Inside decorator, arg1: a
Inside wrapper, arg1: a
This is my function
更多這里: https://www.geeksforgeeks.org/decorators-with-parameters-in-python/
添加回答
舉報(bào)