How to create a list of objects? How to create a list of objects? python python

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 = numbermy_objects = []for i in range(100):    my_objects.append(MyClass(i))# laterfor obj in my_objects:    print obj.number


You can create a list of objects in one line using a list comprehension.

class MyClass(object): passobjs = [MyClass() for i in range(10)]print(objs)


The Python Tutorial discusses how to use lists.

Storing a list of classes is no different than storing any other objects.

def MyClass(object):    passmy_types = [str, int, float, MyClass]