我在 Python 中创建了一个只有抽象方法的类,然后实现了该类中的所有方法BasicMR
(片段中并非所有代码BasicMR
都存在,但该方法test_stationary()
在内部被调用self.generate_positions()
):
class Strategy(object):
__metaclass__=ABCMeta
@abstractmethod
def generate_positions(self):
raise NotImplementedError("You must implement generatePositions()!")
@abstractmethod
def get_positions(self):
raise NotImplementedError("You must implement getPositions()!")
class BasicMR(Strategy):
def __init__(self, security, lookback=30):
self.__securities = []
self.__securities = security # it could be either one security or a list of security objects
self.__positions = self.generate_positions()
def test_stationary(self, lb=30):
if not isinstance(self.__securities, list):
securities = [self.__securities]
else:
securities = self.__securities
prices = [one_stock.get_prices(lb) for one_stock in securities] # list comprehension
results = [ts.adfuller(price_series, regression="c", autolag='AIC') for price_series in prices]
simple_results = [BasicMR.simplify_adf_results(result[1], result[0], result[4]) for result in results]
if len(simple_results) == 1:
return simple_results[0]
else:
return simple_results
def generate_positions(self, long_only=False, time_frame=20, max_holding_period = 20):
result = []
names = []
# HERE IS SOME CODE
return [names, result]
def get_positions(self):
return self.__positions
但是,当我创建 的实例时BasicMR()
,我在这一行得到一个错误:
if not isinstance(self.__securities, list):
错误:
AttributeError: 'int' object has no attribute '_BasicMR__securities'
不知何故,我的 BasicMR 类实例被识别为 type int
,我不知道如何处理该错误。