实现python装饰器以将该函数传递给另一个函数

时间:2020-09-28 21:26:02

标签: python python-decorators

我想设计一个python decorator,它带有一个经过修饰的函数并将其传递给另一个函数。这是我要做什么的代码说明:

def run_decorator(run_batch):                                                                                                
    # inner function can access the outer local                                                                            
    # functions like in this case "func"                                                                                   
    def check(command_to_run):                                                                                             
        @functools.wraps(command_to_run)                                                                                             
        def wrapper(*args, **kwargs):
            batch_json_path = kwargs['batch_json_path']                                                                    
            batch_name = kwargs['batch_name']                                                                              
            folder_path = kwargs['folder_path']                                                                            
            if batch_json_path is not None:                                                                                
                if batch_present(batch_json_path, batch_name):
                    run_batch(batch_json_path, command_to_run, batch_name)
         return wrapper
    return check

def run_batch(batch_abs_path, command_to_run, batch_name=None):
    with open(batch_abs_path) as json_file:
        variant = ...
        tag_data = ...
        command_to_run(variant, batch_name, tag_data)

@run_decorator(run_batch=run_batch)                                                                                                         
def load_tag_for_variant(variant, batch_name, tag_data):

这样的行为可能实现吗?任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

您在评论中指出:Where

据此我了解到,您想在装饰时传递这些参数。为此,您应该将它们传递给I need to pass to the decorator 3 parameters: batch_json_path, batch_name and folder_path which are not the parameters of the load_tag_for_variant function, but should somehow be fed into the decorator / load_tag_for_variant at the moment program runs.函数。

run_decorator

您还希望将用于调用修饰函数的args(def run_decorator(run_batch, batch_json_path, batch_name, folder_path): def check(command_to_run): @functools.wraps(command_to_run) def wrapper(*args, **kwargs): if batch_json_path is not None: if batch_present(batch_json_path, batch_name): run_batch(batch_json_path, batch_name, command_to_run, *args, **kwargs) return wrapper return check @run_decorator(run_batch, batch_json_path="a_path", batch_name="a_name", folder_path="a_path") def load_tag_for_variant(variant, batch_name, tag_data): ... )和kwargs传递给原始函数(variant, batch_name, tag_data / load_tag_for_variant)。使用示例功能,您可以这样完成:

command_to_run

鉴于您有两次def run_batch(batch_abs_path, batch_name, command_to_run, *args, **kwargs): command_to_run(*args, **kwargs) ... ,我不确定是要在修饰时还是在函数调用时进行定义。例如,如果您只想从kwargs中获取它,可以执行batch_name