为什么我的 class 实例是 int 类型的(我无法访问它的属性)?
Why my class instance is of type int (I can't access its attributes)?
我在Python中创建了一个只有抽象方法的class,然后实现了classBasicMR
中的所有方法(不是所有的代码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 class 实例被识别为 int
类型,我不知道如何处理该错误。
您正在 test_stationary
class:
>>> BasicMR.test_stationary(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 20, in test_stationary
AttributeError: 'int' object has no attribute '_BasicMR__securities'
这将作为 self
参数传入 42
,因为该方法未绑定。
您不能创建 class 的实例,因为您没有为generate_positions
和get_positions
提供具体实现,但是:
>>> BasicMR([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 17, in __init__
File "<string>", line 6, in generate_positions
NotImplementedError: You must implement generatePositions()!
终于找到问题的根源了。在方法 generate_positions()
中,我调用了实例的方法,就好像它是一个 class 方法一样,而不是写
stationarity_checked_securities = self.test_stationary(time_frame)
我有:
stationarity_checked_securities = BasicMR.test_stationary(time_frame)
test_stationary()
定义为:
def test_stationary(self, lb=30):
#some code here
也就是说,它不是class方法,但我把它称为好像是。
最后,我的错误看起来很简单,但我对错误文本感到困惑(我是 Python 的新手,我不明白为什么我的 class 实例被称为错误中的“int object
”。
我在Python中创建了一个只有抽象方法的class,然后实现了classBasicMR
中的所有方法(不是所有的代码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 class 实例被识别为 int
类型,我不知道如何处理该错误。
您正在 test_stationary
class:
>>> BasicMR.test_stationary(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 20, in test_stationary
AttributeError: 'int' object has no attribute '_BasicMR__securities'
这将作为 self
参数传入 42
,因为该方法未绑定。
您不能创建 class 的实例,因为您没有为generate_positions
和get_positions
提供具体实现,但是:
>>> BasicMR([])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 17, in __init__
File "<string>", line 6, in generate_positions
NotImplementedError: You must implement generatePositions()!
终于找到问题的根源了。在方法 generate_positions()
中,我调用了实例的方法,就好像它是一个 class 方法一样,而不是写
stationarity_checked_securities = self.test_stationary(time_frame)
我有:
stationarity_checked_securities = BasicMR.test_stationary(time_frame)
test_stationary()
定义为:
def test_stationary(self, lb=30):
#some code here
也就是说,它不是class方法,但我把它称为好像是。
最后,我的错误看起来很简单,但我对错误文本感到困惑(我是 Python 的新手,我不明白为什么我的 class 实例被称为错误中的“int object
”。