繁体   English   中英

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

[英]Implement python decorator for passing down the function to another function

我想设计一个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):

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

您在评论中指出: 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函数。

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

您还希望将用于调用装饰函数的 args( variant, batch_name, tag_data )和 kwargs 传递给原始函数( 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)
    ...

鉴于您在那里有两次batch_name ,我不确定您是希望在装饰时还是在函数调用时定义它。 例如,如果您只想从 kwargs 获取它,则可以执行kwargs.get('batch_name')

暂无
暂无

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

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