实现 ABC 子类的正确方法 [英] Proper way to implement ABC SubClass

查看:33
本文介绍了实现 ABC 子类的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接口类,它定义了一个活动的使用中"类的要求:

I have an Interface class which defines the requirements to an active "in-use" class:

class Portfolio(ABC):
    @abstractmethod
    def update_portfolio(self):
        raise NotImplementedError

    @abstractmethod
    def update_from_fill(self):
        raise NotImplementedError

    @abstractmethod
    def check_signal(self, signal_event):
        raise NotImplementedError

update_portfolio 和 update_from_fill 这两个方法在 99% 的必需情况下都是相同的.只有 check_signal 方法会有所不同.因此,为了避免重复编写相同的代码,我为 update_portfolio 和 update_from_fill 定义了一个带有默认方法的基类:

The methods update_portfolio and update_from_fill are both methods which will be the same in 99% of the required cases. Only the check_signal method will vary. Therefore, to avoid having to write the same code again and again, I have defined a base class with default methods for update_portfolio and update_from_fill:

class BaseBacktestPortfolio(Portfolio):
    def __init__(self, ...):
        ...

    def update_portfolio(self, ...):
        ...

    def update_from_fill(self, ...):
        ...

最后,我有一个继承自 BacktestPortfolio 类的类,它指定了 check_signal 方法的正确实现:

Then, finally, I have a class inheriting from the BacktestPortfolio class which specifies the correct implementation of the check_signal method:

class USBacktestPortfolio(BaseBacktestPortfolio):
    def check_signal(self, ...):
        ...

现在,问题是我的编辑器抱怨 BacktestPortfolio 分类没有所有必需的抽象方法.当然,我可以忽略这一点,但最理想的情况是,如果我能确保无法从 BacktestPortfolio 类中实例化对象.

Now, the problem is that my editor complains about the BacktestPortfolio classing not having all the required abstract methods. I could ignore this, of course, but the perfect scenario would be if I could make sure that it is not possible to instantiate an object form the BacktestPortfolio class.

这可能吗?和/或是否有更正确的方法来实现这样的结构?

Is this possible? And/or is there a more correct way to implement a structure like this?

推荐答案

您可以将 BaseBacktestPortfolio 也作为抽象类.

You can make the BaseBacktestPortfolio also as Abstract class.

from abc import ABC, abstractmethod

class Portfolio(ABC):
    @abstractmethod
    def update_portfolio(self):
        pass

    @abstractmethod
    def update_from_fill(self):
        pass

    @abstractmethod
    def check_signal(self, signal_event):
        pass

class BaseBacktestPortfolio(Portfolio, ABC):

    def update_portfolio(self):
        print("updated portfolio")

    def update_from_fill(self):
        print("update from fill")

    @abstractmethod
    def check_signal(self):
        pass

class USBacktestPortfolio(BaseBacktestPortfolio):
    def check_signal(self):
        print("checked signal")

另请注意,您不需要在抽象方法中raise NotImplementedError.您只需pass.它更像 Pythonic :)

Also notice that you don't need raise NotImplementedError inside abstract method. You can just pass. Its more Pythonic :)

这篇关于实现 ABC 子类的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆