繁体   English   中英

Append class 实例到 Python 中的列表

[英]Append class instance to a list in Python

我在 object 上创建了一个实例

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_case = test_case.TestCase()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM