2016-10-04 199 views
-1

我定義了響應類這樣的:Python類的實例變量消失

class Response(object): 
    def __index__(self): 
     self.country = "" 
     self.time_human = "" 
     self.time_utc = "" 
     self.text = "" 
     self.time_object = None 
     self.clean_word_list = [] 

    def parse_line(self, line): 
     if 'text:' in line[:10]: 
      self.text = line[7:].strip() 
     elif 'country: ' in line[:9]: 
      self.country = line[8:].strip() 
     elif 'time_human: ' in line[:15]: 
      self.time_human = line[12:].strip() 
     elif 'time_utc: ' in line[:15]: 
      self.time_utc = int(line[10:].strip()) 
      self.time_object = datetime.fromtimestamp(self.time_utc) 

我然後有讀取從文本文件線和適當的值分配給該響應的方法:

class file_importer(object): 
    def __init__(self, file_name): 
     self.file_name = file_name 

def get_responses_from_file(self): 
    directory = DIRECTORY_TO_FILE 
    formatted_filename = directory + self.file_name 
    file = open(formatted_filename, 'r') 
    response = Response() 
    response_list = [] 
    for line in file: 
     if line[0] == '*': 
      response_list.append(response) 
      response = Response() 
     else: 
      response.parse_line(line) 
    return response_list 

但get_responses_from_file()返回的response_list是沒有response.clean_word_list屬性的響應列表。發生了什麼?

+0

構造被稱爲'__init__'和** **不是'__index__' – Wombatz

回答

0

它必須是__init__而不是__index__Response

+0

謝謝!我絕對誤讀了這條線。德古拉顏色主題和缺乏睡眠的組合是一個糟糕的組合。感謝您爲我指出了這一點 – Ian