user225312 user225312 - 1 year ago 79
Python Question

Creating a list of objects in Python

How do I go about creating a list of objects (class instance) in Python?

Or is this a result of bad design? I need this cause I have different objects and I need to handle them at a later stage, so I would just keep on adding them to a list and call them later.

Answer Source

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):

# later

for obj in my_objects:
    print obj.number
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download