Python:普通属性错误
修复:编辑并保存导入的模块后必须重新启动编辑器。。。我每次都得这么做吗?我使用的是EnThound Canopy(64位) 今天我刚开始学习Python类,我想知道为什么在我尝试从我的股票类调用returnPlot()之后会出现属性错误。pricePlot()有效 我已经检查了一次又一次的缩进,并重新标注了所有内容,但我确信这将是一个愚蠢的问题 下面是Stock.py:Python:普通属性错误,python,attributeerror,Python,Attributeerror,修复:编辑并保存导入的模块后必须重新启动编辑器。。。我每次都得这么做吗?我使用的是EnThound Canopy(64位) 今天我刚开始学习Python类,我想知道为什么在我尝试从我的股票类调用returnPlot()之后会出现属性错误。pricePlot()有效 我已经检查了一次又一次的缩进,并重新标注了所有内容,但我确信这将是一个愚蠢的问题 下面是Stock.py: class Stock: # Graphically presents the time trends of pri
class Stock:
# Graphically presents the time trends of prices of the stock
def pricePlot(self):
## Download the data
dataFrame = self.priceDownload()
...
# Graphically presents the time trends of the return of the stock
# Parameter: Interval - the change in time; represented in days
def returnPlot(self, interval):
## Download the data
dataFrame = self.priceDownload()
## Get the x-values we will be using
xAxis = [dataFrame[0][0]]
index = interval
while(index < len(dataFrame[0])):
xAxis.append(dataFrame[0][index])
index=index+interval
## Compute the return
returns = [0]
index = interval
while(index < len(dataFrame[2])):
returns.append(np.log2(dataFrame[2][index]/dataFrame[2][index-interval]))
index=index+interval
## Plot the return
fig, ax = plt.subplots()
yAxis = returns
## Date setting
months = date.MonthLocator()
years = date.YearLocator()
fmt = date.DateFormatter("%Y")
plt.plot_date(xAxis, yAxis,'-')
## Configure the x-axis
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(fmt)
ax.xaxis.set_minor_locator(months)
ax.autoscale_view()
ax.grid(True)
fig.autofmt_xdate()
## Add text description to the figure
plt.xlabel("Date")
plt.ylabel("Return")
plt.title('Returns of ' + self.ticker)
plt.show()
## Save the figure
full_path = os.path.realpath(__file__)
path, file = os.path.split(full_path)
plt.savefig(path + '\\' + self.ticker + 'returns.pdf')
这是回溯:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
main.py in <module>()
5
6 myStock.pricePlot()
----> 7 myStock.returnPlot(5)
AttributeError: Stock instance has no attribute 'returnPlot'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
main.py in()
5.
6 myStock.pricePlot()
---->7 myStock.returnPlot(5)
AttributeError:股票实例没有属性“returnPlot”
提前感谢。您不能指望
python
不断地重新读取每个导入的模块,并使用对文件的任何新更改更新导入的模块python
在导入时读取每个模块文件一次
如果您使用的是ipython
,则可以在使用reload(module)
编辑模块后重新加载模块,其中module
是模块的名称。每次编辑模块时,都需要重新加载模块。范例
from imp import reload
reload(s)
这些行对任何python终端或代码都有效(不仅仅是起诉ipython)。有关的更多信息。请缩短您的问题,使其仅包含重现您的问题所需的代码。您是否可以发布returnplot的代码。我觉得这像是缩进错误。你确定你的def returnPlot在你的库存下吗class@Nick:如图所示,它位于pricePlot()函数的正下方。实际上,在查看主代码后,您可以看到这不是同一代码。如果要传递Stock参数,必须在Stock类中定义\uuuu init\uuuu
方法。
from imp import reload
reload(s)