2015-05-02 52 views
-2

修復:編輯並保存導入的模塊後不得不重新啓動編輯器...每次都必須執行此操作嗎?我正在使用Enthought Canopy(64位)Python:微不足道的屬性錯誤

我剛剛開始使用Python類,並且想知道爲什麼在嘗試從我的Stock類調用returnPlot()後出現屬性錯誤。 pricePlot()起作用。

我已經檢查了一遍又一遍的縮進,並重申了一切,但我相信這將是一個愚蠢的問題。

這裏是Stock.py:

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') 

這裏是main.py - 調用類股票代碼:

import Stock as s 


myStock = s.Stock("VFINX") 

myStock.pricePlot() ## WORKS 
myStock.returnPlot(5) ## THROWS ERROR 

這裏是回溯:

--------------------------------------------------------------------------- 
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' 

提前致謝。

+1

請編輯您的問題並縮短它,以便它只包含重現問題所需的代碼。 –

+0

你可以發佈你的代碼returnplot。 –

+1

這對我來說看起來像是一個縮進錯誤。你確定你的def returnPlot是否在你的庫存檔下 –

回答

0

您不能指望python不斷重新讀取每個導入的模塊,並更新導入的文件以及對這些文件所做的任何新更改。 pythonimport時讀取每個模塊文件一次。

如果您使用ipython,你可以用reload(module),其中module是你的模塊的名稱編輯之後重新加載的模塊。

0

每次你做一個版本,你需要重新加載你的模塊。示例

from imp import reload 
reload(s) 

這些行適用於任何python終端或代碼(不僅僅是起訴ipython)。有關imp module的詳細信息。