0

Here is the code to start with.

Ignoring all the CSV jazz if you want, the important part is the loop.

import csv

Teacher_Array = []

class Teacher:

    def __init__ (self, login, password, name, room, booked):
        self.password = password
        self.name = name
        self.login = login
        self.booked = booked
        self.room = room


with open('file.csv') as f:

    reader = csv.reader(f)
    for row in reader:
        if len(row) < 10 and len(row) > 2:

Ok.

The cvs stuff is a little besides the point, the key info is the loop, how do I create different variable names each time?

At this point I want to create teacher objects say,

MrDongle = Teacher(row[0], row[1], row[3], etc)

However I can't make a new name in the loop! Help please.

Much love, some dumb teenager.

Shahzad
  • 2,033
  • 1
  • 16
  • 23
  • 1
    Maybe you would like to put them into a `dict`? `some_dict['MrDongle'] = Teacher(...)` – mostruash Nov 23 '14 at 11:28
  • 1
    Creating a bunch of variable names might not be the best solution. You could just collect the object instances in a list. Depending on what you intend to do with the results, this might be prefered to storing a dictionary, as suggested before. – Isaac Nov 23 '14 at 14:16

1 Answers1

0

Well, just make a name a key in a dictionary:

teachers = {}
....
teachers['MrDongle'] = Teacher(row[0], row[1], row[3], etc)

EDIT Actually, you can generate a name:

exec('{} = Teacher(...)'.format('MrDongle', ...)

but this is unscalable and improper solution.

volcano
  • 3,578
  • 21
  • 28