覆盖方法但保留decorator(python)

2024-06-28 20:46:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个类和一个子类,并希望从frameworkperfict.io继承一个decorator@task方法

代码示例:

课程

@task
def test1(self):
     pass

子类

def test2(self):
     print("succeed")

但是现在方法test2不再具有decorator@task。我不能在子类中声明@task。我是否可以覆盖该方法,但保留@task


Tags: 方法代码ioself示例taskdefdecorator
2条回答

我通常推荐这样的东西

class Base:
    @task
    def test1(self):
        return self.test1_impl()

    def test1_impl(self):
        ...


class Child:
    def test1_impl(self):
        ...

现在Child的工作不是重写修饰函数,而是继承的修饰函数所使用的未修饰函数

为什么不再次导入任务

from prefect import task

相关问题 更多 >