0

Could someone please explain to me how to create class instances from a list (or a string that might be fetched from excel). I always seem to run into this problem. I want to create many instances of classes and then save them in a shelve later.

This sample code doesn't work, but illustrates the approach I'm trying.

class test:
  def __init__(self):
      self.a='name'

if _name__=='__main__':
   list=['A','B']
   for item in list:
       item=test()
9
  • When you call "for i in seq:", the i isn't the original object in seq, but a copy to it. So your code wouldn't work. Commented Nov 30, 2010 at 0:40
  • 2
    Why are you trying this? Where have you seen class used as a function? What tutorial are you following to learn Python? Commented Nov 30, 2010 at 0:41
  • Sorry all, new to python and new to Stackoverflow. Please excuse my struggles. Commented Nov 30, 2010 at 0:51
  • I know that that if I type A=test() A will be an instance of test, and A.a='name'. In my program, the name of the class instance will be entered by the user (in excel) and I need to create the class instance from this string value. Commented Nov 30, 2010 at 0:51
  • 2
    I cannot recall one point in time from when I first started programming to now where I though that it was a good idea to have the name of an instance be dependent upon what should be an instance variable. If you have a class to keep track of users, name it User. An instance could be user or active_user or banned_user etc. There's no good reason for the an instance to be named fred. Commented Nov 30, 2010 at 1:05

1 Answer 1

1

Few issues in your code includes naming of variables. It can confuse you.

class test:
    # I guess you want to provide the name to initialize the object attribute
    def __init__(self, name):   
        # self.name is the attribute where the name is stored.
        # I prefer it to self.A 
        self.name = name        

Now the issue here is that instance is also a element of your list, which I presume is a name.

if __name__=='__main__':
    # I presume these are list of names
    list_of_names = ['A','b','c']

    # You have to store your instance some where.
    instance_list = []

    # Here name is an element of the list that you are iterating
    # I change it to name instead of instance
    for name in list_of_names:   
        # Here I am appending to the list, a test object that I create         
        instance_list.append(test(name))

[Edit:]

Now, I truly don't understand you, why this piece of code:

  for item in list:
       item=class()   # How can you reassign the item ? 

See what this item is .

>>> for item in ['A', 'B']:
...     print item
... 
A
B
>>> 

You should not assign it item = .... but you should use it .... = ..(item) !!!

Sign up to request clarification or add additional context in comments.

2 Comments

Each class instance will get variables from somewhere else and perform functions. What I'm stuck on is how to create instances from a fetched string value (or a list as in this case)
@Todd: what does "create instances from a fetched string value mean? Instances aren't created from anythin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.