简体   繁体   English

如何在Beaker中将变量传递给createfunc

[英]How do I pass a variable to createfunc in Beaker

In the Beaker documentation, they talk about not passing a parameter directly in the createfunc call but use a closure. 在Beaker文档中,他们谈论的是不直接在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: 可以通过使用创建函数上的闭包范围来传递影响所创建值的选项:

All examples and documentation I can find on closure hint toward a nested function call with the first taking in a variable. 我可以在闭包提示中找到有关嵌套函数调用的所有示例和文档,其中第一个带有变量。 In this case I don't understand how to write the closure since its not a function but a key value variable. 在这种情况下,我不了解如何编写闭包,因为它不是函数而是键值变量。

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

How would I pass variable_a into get_results(variable_a) in the createfunc ? 我怎么会通过variable_aget_results(variable_a)createfunc

Like so, or similar? 是这样还是类似?

get_results_func returns a function pointer that, because it's in a closure, will call get_results correctly. 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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