[英]Python decorator with arguments that depends on the decorated function
我有這樣裝飾的功能:
@do_something(cache_key=CACHE_ID ,timeout=CACHE_ID_TIMEOUT)
def get_something_from_cache():
...
...
...
return result
我的裝飾:
def do_something(function=None, cache_key='', timeout=300):
def decorator(func):
@wraps(func, assigned=available_attrs(func))
def inner(*args, **kwargs):
...
...
...
return decorator if function is None else decorator(function)
我希望能夠有不同的'cache_key'取決於修飾的功能輸入。 就像是:
@do_something(cache_key=CACHE_ID.format(att) ,timeout=CACHE_ID_TIMEOUT)
def get_something_from_cache(att):
...
...
...
return result
可能嗎??
不。
定義修飾的函數后立即執行函數修飾器。 在調用該函數之前, att
並不存在,這可能在定義之后很長時間,或者根本沒有。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.