Frage

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.

War es hilfreich?

Lösung

It seems to me that you are looking for the append method of list:

name = []
name.append(myClass())
name.append(myClass())

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top