2015-09-27 50 views
0

我有以下代碼(片段)...它可能對別人有用......但我有一個問題...我的一個類變量似乎不是工作,好像你可以使用的變量數量有限制?AttributeError:添加新類變量時對象沒有任何屬性

import csv 

    class MASAT_data: 
    # 
    # ALL CSV files are expected to have the first row as headers 
    # ALL CSV files are expected to have the first column as the key attribute 
    # All Headers are extracted into its own object variable 
    # 

    def __init__(self, fname, action, intest): 
     __data_Dict = {} 
     __fname = fname 
     __header = []    # <--- THIS WORKS 
     __colLEN = 0 
     __rowLEN = 0 
     __data_rows = []   # <--- This seems to not be available to the class 
     #__test = intest 
     if action.upper == "NEW": 
      pass 
     else: 
      self.Get_DICT(__fname) 
    def Get_DICT(self,filename): 
     templist = [] 
     daDict = {} 
     with open(filename,"r") as f: 
      csv_f = csv.reader(f) 
      x=-1 
      for row in csv_f: 
       x+=1 
       if len(row)!=0: 
        if x == 0: 
         self.__header = row 
         self.__colLEN = len(self.__header) 
        else: 
         templist = row 
         self.__data_rows.append(templist) # <-- line 38 
         if len(templist) >= 2: 
          daDict[templist[0]] = templist[1:] 
     self.__rowLEN = x 
     f.close() 
     self.__data_Dict = daDict 
    def rtnData(self,keyName): 
     return self.__data_Dict[keyName] 
    def hlen(self): 
     return self.__colLEN 
    def __len__(self): 
     return self.__rowLEN 
    def rtnList(self): 
     templist = [[]] 
     temps = [] 
     for key, value in self.__data_Dict.items(): 
      temps.append(key) 
      for x in value: 
       temps.append(x) 
      templist.append(temps) 
      temps = [] 
     templist.pop(0) 


     #for x in range(len(templist)): 
     # print(templist[int(x)]) 
     return templist 
    def rtnListDict(self): 
     templist = [] 
     for x in range(len(self.__header)): 
      print (self.__header[x]) 
      #print (self.__data_Dict[self.__header[x]]) 

我得到以下錯誤:

C:\Python34\python.exe C:/Users/User/PycharmProjects/MASAT-3/MA-SAT_v3_MAIN.py 
Traceback (most recent call last): 
    File "C:/Users/User/PycharmProjects/MASAT-3/MA-SAT_v3_MAIN.py", line 10, in <module> 
    appCONFIGs = MASAT_data("CONFIG.csv","LOAD",t) 
    File "C:\Users\User\PycharmProjects\MASAT-3\MASAT_Libs.py", line 23, in __init__ 
    self.Get_DICT(__fname) 
    File "C:\Users\User\PycharmProjects\MASAT-3\MASAT_Libs.py", line 38, in Get_DICT 
    self.__data_rows.append(templist) 
AttributeError: 'MASAT_data' object has no attribute '_MASAT_data__data_rows' 

過程與退出代碼完成1

調用程序如下:

__author__ = 'User' 
    import sys 
    from MASAT_Libs import * 

    sys.path.append('C:\\Users\\User\\PycharmProjects\MASAT-3') 
    t = 0 
    #define Globals 

    if __name__ == '__main__': 
     appCONFIGs = MASAT_data("CONFIG.csv","LOAD",t) 
     if appCONFIGs.rtnData("ApplicationMode")[0] == 'DEBUG': 
      t = 1 
     else: 
      t = 0 
     print("t = ",t) 
     observations = MASAT_data(appCONFIGs.rtnData("PathName")[0] + "\\Observables.csv", "LOAD",t) 
     KMNs = MASAT_data(appCONFIGs.rtnData("PathName")[0] + "\\KMN_List.csv", "LOAD",t) 
     geomix = MASAT_data(appCONFIGs.rtnData("PathName")[0] + "\\geomix_perf_list.csv", "LOAD",t) 

     print (geomix.rtnList()) 
     print (len(geomix)) 

     geomix.rtnListDict() 

所以請原諒我放在一個額外的空間,或沒有正確縮進(我在這裏處理代碼時手動完成了...

我的問題是我不能在這個類中加入另一個變量(不管名字是什麼)。任何想法一個簡單的修復?

+0

啊,對了,忘了說了,當我刪除了2號線(在__init__函數的聲明,在線38上的一個...一切都很好...... – Oogobuk

回答

0

適當的方式來聲明實例變量是:

class MASAT_data: 
    def __init__(self, some_var): 
     # like this 
     self.__header = [] 
     self.__data_rows = [] 
     self.__some_var = some_var 

    def some_func(self): 
     # and referenced like this 
     self.__header.append(['test']) 
     self.__data_rows.append(['test']) 
     print(self.__some_var) 

在聲明中__init__一個變量,其範圍僅限於__init__。所以如果你需要在其他地方引用它,它必須有一個更大的範圍。因此self.your_var = []

+0

所以它不是一個靜態的所有類的實例?爲什麼其他的工作? – Oogobuk

+0

感謝Aravindan,這似乎已經帶走了錯誤.. – Oogobuk

+0

好了,錯誤消失了,但是現在這個類的所有實例都包含在這些變量中,我只需要它們是實例,它們在這個位置似乎是靜態的 – Oogobuk

0

好吧,這似乎工作我玩了一下...我真的不得不做的是把self.盈方的所有變量......不知道爲什麼,但似乎是需要?如果有人知道爲什麼是這樣的話,我很想知道的技術原因...

def __init__(self, fname, action, intest): 
    self.__data_Dict = {} 
    self.__fname = fname 
    self.__header = []    # <--- THIS WORKS 
    self.__colLEN = 0 
    self.__rowLEN = 0 
    self.__data_rows = []   # <--- This seems to not be available to the class 
    #__test = intest 
    if action.upper == "NEW": 
     pass 
    else: 
     self.Get_DICT(self.__fname) 
+0

你需要給'self.'加上前綴,否則它們只是一個函數內的局部變量,並且一旦函數已經完成執行了,通過在'self.'前面添加屬性到類實例中,因此它們會一直存在,直到類實例保留在內存中。 –

相關問題