根据Python
中定义Abstract
class
的一些接收,我们应该声明它是这样的:
class BaseStrategy(metaclass=ABCMeta):
但是如果这个类已经从其他类继承了呢?
class BaseStrategy(bt.Strategy):
使用其他选项(基类中的interfaces
,exception
)?
请注意,我试过了:
class BaseStrategy(bt.Strategy, metaclass=ABCMeta):
有了这样的追溯:
Traceback (most recent call last):
File "D:/Projects/trading-bot/main/lab/backtrader/netflix.py", line 13, in <module>
from main.lab.strategy import RSISimple, RSIBuySell, SMACross, SMA_RSI, HolyGrail, MACD_ADX, BBands
File "D:\Projects\trading-bot\main\lab\strategy\__init__.py", line 1, in <module>
from .rsi_simple import *
File "D:\Projects\trading-bot\main\lab\strategy\rsi_simple.py", line 3, in <module>
from main.lab.strategy.base_strategy import BaseStrategy
File "D:\Projects\trading-bot\main\lab\strategy\base_strategy.py", line 6, in <module>
class BaseStrategy(bt.Strategy, metaclass=ABCMeta):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
bt.Strategy也可能使用抽象类。
答案 0 :(得分:3)
错误消息告诉您,您的基类已经有一个不属于您的元类的超类的元类。 Python要求issubclass(B, A)
暗示issubclass(type(B), type(A))
。
你有
class AMeta(type): pass
class A(metaclass=AMeta): pass
class BMeta(type): pass
class B(A, metaclass=BMeta): pass
无效,因为BMeta
(type(B)
)不是AMeta
(type(A)
)的子类。您可以通过声明派生的元类来修复它:
class BMeta(AMeta): pass
# or even
class BMeta(type(A)): pass
class B(A, metaclass=BMeta): pass
在具体案例中,它看起来像这样:
class BaseStrategyMeta(ABCMeta, type(bt.Strategy)): pass
class BaseStrategy(bt.Strategy, metaclass=BaseStrategyMeta): pass