-1

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.

Portadiam
  • 1
  • 2
  • Your wording has severely confused my metaprogramming-damaged brain. Some of my code is actually creating classes at runtime. But I assume you meant instantiating classes, AKA creating objects? –  Oct 13 '12 at 18:57
  • Yes. I mean creating objects at runtime. I shall add that bit of information to the question. – Portadiam Oct 13 '12 at 18:58
  • Well, what have you tried? Surely you have some Python tutorial or other resources -- how are they not helping you? –  Oct 13 '12 at 18:59
  • I have looked around (I started looking half a year ago) but either I do not know the correct wording to use when searching (highly possible) or there is no information for this on the first few pages of Google search results. I have read through the class tutorial of the official python tutorial but this does not contain what I am looking for. – Portadiam Oct 13 '12 at 19:03
  • 3
    Well, the level of your question and your wording suggest that you should be starting the tutorial from the start. You seem to be lacking quite a few basics. –  Oct 13 '12 at 19:04

2 Answers2

1

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

Philipp
  • 535
  • 1
  • 6
  • 16
0

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

name = []
name.append(myClass())
name.append(myClass())
Janne Karila
  • 24,266
  • 6
  • 53
  • 94