If you want to be able to access the objects by name, you can put them in a 
dict. For example,
MyObjects={}
l=["a","b","c"]
for i in l:
        MyObjects[i] = MyClass(i)

Then you can refer to MyObjects["a"]

If all the operations on the objects are done to all objects in a batch, putting them in a list might work. Then you can use list iteration to access all of them:
MyObjects=[]
l=["a","b","c"]
for i in l:
MyObjects.add(MyClass(i))


then to process all the objects:
for myObj in MyObjects:
    # do something with myObj

Kent


Matt Williams wrote:
Dear Tutor-list,

I'm sorry for this appallingly dumb question, but I'm having a little
problem with objects.

I've written a class, with some methods. I then want to be able to call
the class repeatedly, to create some objects. The number of objects, and
some of their initialisation parameters need to be specified later (i.e.
at run-time).

When I generate all these objects, how do I keep track of them. For a
finite (and small) number I can do this:

a=MyClass("a")
b=MyClass("b")

but this is obviously not scaleable. If I use a list, I can do:

MyObjects=[]
l=["a","b","c"]
for i in l:
        MyObjects.add(MyClass(i))

but then I have to search the list (MyObjects) for the object where
Object.name="a".

The only other option seems to be finding objects via their hash codes,
which I'm sure isn't right

I'd like to be able to automate something closer to the a=MyClass("a")
but don't know how to do it....It may be that I'm trying to do it very
badly, which is Python seems to make it hard for me.

Thanks,

Matt



_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to