How to create a list of objects?
Storing a list of object instances is very simple
class MyClass(object):
def __init__(self, number):
self.number = number
my_objects = []
for i in range(100):
my_objects.append(MyClass(i))
# later
for obj in my_objects:
print obj.number
You can create a list of objects in one line using a list comprehension.
class MyClass(object): pass
objs = [MyClass() for i in range(10)]
print(objs)
In Python, the name of the class refers to the class instance. Consider:
class A: pass
class B: pass
class C: pass
lst = [A, B, C]
# instantiate second class
b_instance = lst[1]()
print b_instance
The Python Tutorial discusses how to use lists.
Storing a list of classes is no different than storing any other objects.
def MyClass(object):
pass
my_types = [str, int, float, MyClass]