Pergunta

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.

Foi útil?

Solução

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

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

Outras dicas

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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top