[Tutor] Creating & Handling lots of objects

Matt Williams matthew.williams at cancer.org.uk
Fri Dec 3 12:50:55 CET 2004


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





More information about the Tutor mailing list