在Python中将类实例附加到列表中
我已经在一个对象上创建了一个实例在Python中将类实例附加到列表中,python,list,append,instance,Python,List,Append,Instance,我已经在一个对象上创建了一个实例 for row in csv_reader: temp_case = test_case.TestCase temp_case.file = row[1].strip() ... test_cases.append(temp_case) print(test_cases[0].file) 正如您所看到的,我将实例附加到一个列表中,我可以在其他模块中对其进行迭代,并且我正在检查列表中的第一项是什么 。。。每次迭代我都会得
for row in csv_reader:
temp_case = test_case.TestCase
temp_case.file = row[1].strip()
...
test_cases.append(temp_case)
print(test_cases[0].file)
正如您所看到的,我将实例附加到一个列表中,我可以在其他模块中对其进行迭代,并且我正在检查列表中的第一项是什么
。。。每次迭代我都会得到一个不同的名称,但我希望它是相同的:
test1.py
test2.py
test3.py
顺便说一句,整个列表是同一个实例
print(test_cases[0].file)
print(test_cases[1].file)
print(test_cases[2].file)
结果如下:
test3.py
test3.py
test3.py
显然,以下解决了这个问题:
temp\u case=test\u case.TestCase()