用两个功能装饰一个功能

时间:2020-03-27 14:17:12

标签: python-3.x decorator python-decorators

我想使用带有两个功能的阅读装饰器。第一个函数应该告诉我们要使用“ open(path,filename)”还是“ z.open(path,filename)”,另一个函数指定读取逻辑。但是,我只知道如何装饰第一个功能,请参见下文

0

我们需要声明

def reader(func):

    def wrapper(*args,**kwargs):

        with func(*args,**kwargs) as f:

             reading_logic(f)

然后:

@reader
def file_reader(*args,**kwargs):
    return open(*args, **kwargs)

但是,我还想装饰“阅读器”中的阅读逻辑。因此,它会根据我介绍的文件格式而变化。 谢谢

0 个答案:

没有答案