简体   繁体   中英

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.

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 ?

Like so, or similar?

get_results_func returns a function pointer that, because it's in a closure, will call get_results correctly.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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