문제

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.

도움이 되었습니까?

해결책

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

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top