[Tutor] Some advice on classes needed ?

Glen Wheeler gew75 at hotmail.com
Sat May 15 11:12:37 EDT 2004

> [..]
> But I have just read about classes :-)
> If I design a class for one set of data, how do I generate all the
> instances of classes without 250 generate instances.
> data1=datastruct()
> data2=datastruct()
> ...
> data250=datastruct()
> Following on, how can I sweep the 250 data structures instances to get
> statistical data without manually doing 250 instance calles.
> What I am asking is, Is it possible to use a for loop ? ie dynamicly
> generate an instance name to then call ?

  What you are asking here is not really a nice thing to do in code.
Dynamically creating an instance variable *name* involved the use of eval;
which is not a good idea here.

  I'd suggest something more along the lines of what Alan Gauld stated; use
a list or a dictionary.

  As opposed to simply handing out the solution (where is the fun in that?)
here is a little (big?) hint:

data = []
for i in range(250):

  So this is a list of i, from 0 to 250.  Noting that unlike most languages
python's list object can hold any number of *different* types, one could
construct a list-based solution to your problem.

  Dictionaries are of course alot more fun though.  Let's leave that as an
exercise... ;).

> Hopefully this is an intelegent question - this is the first time I have
> ever tries OOP ! :-)

  I hope you like it -- it's one of the things python does *really* well.


More information about the Tutor mailing list