Pythonでインスタンスを動的に生成したい
解決したいこと
インスタンスをfor文で動的に生成したが、干渉し合っている?
発生している問題
想定した動作
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
実際の動作
[0, 1, 1, 2, 2, 3, 3]
[0, 1, 1, 2, 2, 3, 3]
[0, 1, 1, 2, 2, 3, 3, 3, 3]
[0, 1, 1, 2, 2, 3, 3, 3, 3]
[0, 1, 1, 2, 2, 3, 3, 3, 3, 2, 2, 3, 3]
[0, 1, 1, 2, 2, 3, 3, 3, 3, 2, 2, 3, 3]
[0, 1, 1, 2, 2, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3]
[0, 1, 1, 2, 2, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3]
該当するソースコード
class Recursive:
def __init__(self, max_limit: int, times: int, recursive_count: int = 0, count_list: list = None):
self._max_limit: int = max_limit
self._times: int = times
self._recursive_count: int = recursive_count
if count_list is None:
self._count_list: list = []
else:
self._count_list: list = count_list
self._count_list.append(self._recursive_count)
self._instances_list: list = []
def __generator(self):
for _ in range(self._times):
_instance: object = Recursive(self._max_limit, self._times, self._recursive_count + 1, self._count_list)
self._instances_list.append(_instance)
def __executor(self):
for _instance in self._instances_list:
_instance.start()
def start(self):
if self._max_limit == self._recursive_count:
print(self._count_list)
return 0
self.__generator()
self.__executor()
def main():
test = Recursive(3, 2)
test.start()
if __name__ == '__main__':
main()
0