Python类实例变量消失了

时间:2016-10-04 03:29:48

标签: python class

我像这样定义响应类:

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属性的响应列表。发生了什么事?

1 个答案:

答案 0 :(得分:0)

__init__班级必须为__index__而不是Response