[Tutor] Creating class instances through iteration

Tim Goddard timgoddardsemail at gmail.com
Fri Apr 16 04:03:52 CEST 2010


I came across a situation where what I thought I wanted to do was to create
a class that was spawned from data in a .csv file.  Where column 1 was the
name I wanted to use for each instance.  I had it all figured out and
working except for how to write a statement where the left hand side could
be a changing identifier.

All I could figure out was how to create the same instance 50 times albeit
in different ways.

For example each row would look like [name, value1, value2, value3], I
planned on passing the three values as a tuple

The code would simply be:

for row in csvimport:
    tuple = (row[1],row[2],row[3])
    instancename = Classname(tuple)

How could I create different instances inside an iteration loop for each row
?  Is it possible to change the name through modification of self attribute
(wait is self an attribute?)  Are cats sleeping with dogs here or what?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100415/6a1c89b3/attachment.html>


More information about the Tutor mailing list