Multidimensional list of classes - overwriting issue
问题 I want to create a list of classes, but every time I change an element in the list, all class elements in the list are overwritten: class TypeTest: def __init__(self): self.val = int() data = list([TypeTest for _ in range(3)]) for i in range(3): data[i].val = i print([data[0].val, data[1].val, data[2].val]) At the end, I need a multidimensional array and will have a more complex class, but the issue is the same. 回答1: TypeTest is a type; TypeTest() creates an instance of that type. You need