如何将变量传递给B中的createfunc

2024-04-28 09:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在烧杯文档中,他们讨论了不在createfunc调用中直接传递参数,而是使用闭包。你知道吗

The creation function must not accept any arguments as it won’t be called with any. Options affecting the created value can be passed in by using closure scope on the creation function:

我可以在闭包中找到的所有示例和文档都指向一个嵌套函数调用,其中第一个引用了一个变量。在这种情况下,我不明白如何编写闭包,因为它不是一个函数,而是一个键值变量。你知道吗

results = tmpl_cache.get(key=search_param, createfunc=get_results)

如何将variable_a传递到createfunc中的get_results(variable_a)?你知道吗


Tags: the文档参数getnotanyfunctionbe
1条回答
网友
1楼 · 发布于 2024-04-28 09:23:14

像这样,还是类似的?你知道吗

get_results_func返回一个函数指针,由于它位于闭包中,因此将正确调用get_results。你知道吗

def get_results_func(variable_a):
    def call_get_results():
        return get_results(variable_a)
    return call_get_results  # note the absence of brackets here.

results = tmpl_cache.get(key=search_param, createfunc=get_results_func(variable_a))

相关问题 更多 >