Creating an unbounded number of objects in Python 2.7.3 [closed]
-
07-07-2021 - |
Pregunta
I have started to make an artificial life simulator in Python 2.7.3 but I am unable to create classes at runtime, continuously. I know that I can do this in C++ by storing the class in a list. Does anyone know how to do this? I mean something like this:
x = 1
name = list()
name[x] = myClass()
x+=1
name[x] = myClass()
And so on, although I will be using a loop for this.
Solución
It seems to me that you are looking for the append
method of list
:
name = []
name.append(myClass())
name.append(myClass())
Otros consejos
You cannot append items like this to a python list. Do it like
name = list()
name.append(myClass())
Please be sure to read up on how the basic constructs of python work http://docs.python.org/tutorial/datastructures.html and http://docs.python.org/tutorial/classes.html
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow