Python: Generating instances automaticly from imported data

I am coding a sort of timetabling thing in python and need some advice.

i have a bunch of data - the following is an example:

xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx
vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv vvv
xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx

basicly its a big block of integers but they all have specifc meaning and i need them all. So i import the integers from a text file into python then, split them all and add store it as a list. so i now have a list of integers:

print listOfInt

yields: 'xxx', 'xxx', 'xxx' ........ 'vvv', 'vvv', 'vvv'.....'xxx', 'xxx', 'xxx'

this is all good and is working how i want it to.

now i have a class setup that takes 1 value from the 'vvv'  (vvv can be thought of as a unique identifer) section of the data and some values from the 'xxx' set of data.

so i generate an instance of my class on one occaison. the instance is named 'vvv'. However i have many 'vvv's like a few hundred. the order of the imported data is all preknown.

Is there a way of automaticly generating instances of my class from the imported data? as in can i create instances of a class from my list and if so how would i go about doing this?

heres a pseudo code of kind of what i want.

for item in list:
   item = ClassName(xxx1, xxx2, xxx3)

where item = 'vvv'

so i end up with a bunch of instances of ClassName all called whatever there specific value of 'vvv' is initiated with a bunch of the xxx values.

Sorry for the really long winded post... i really dont know how to go about explaining it better. Thanks for any help you can offer.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for item in list:
   locals()[item] = ClassName(xxx1, xxx2, xxx3)

Of course, item has to be a valid Python identifier. If it isn't (all digits, say) you'll need to make it valid by putting a _ on the front.

If that is not feasible then:

items = {}
for item in list:
   items[item] = ClassName(xxx1, xxx2, xxx3)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Basically, everything in Python is an object. When creating the object of a class, you get the untyped reference to the object. The type is bound to the object, not to the variable that it is assigned to.

Variables in Python are just names stored in a hidden, internal dictionary (the key) bound to the value of the reference (the dictionary item value. (Dictionary is a look-up table, a.k.a. hash table,...).

However, you probably would like to access your 'vvv' objects using their string/number/whatever hashable value. They probably are not hardcoded in the application. This way the second approach presented by PaulKeating is probably more suitable. Then you can call the methods the object the way like:


You can also process all objects via iterating through the items dictionary like:

for name, obj in items.iteritems():
    if name == 'vvv':
To add, the first approach by PaulKeating shows using the internal dictionary (with limitations for the key value related to what the identifier in Python can contain). The second approach uses your own, explicit dictionary.
AshweeAuthor Commented:
Thanks guys, i wont be able to check this till tommorow (audit day at work) but it looks like its exactly what im after. I will distribute points and supply further feedback in a day or so.

Thanks again :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.