1

I want to create instance objects automatically as I explained in the following:

Class MyClass:
     def __init__(self,x):
           self.x = x

list  = ["A","B"]

I want to create the following but automatically, means to loop through the list and create identical object for each element:

A = MyClass(text)
B = MyClass(text)

e.g. like the following which doesn't work:

# this doesn't work but explains more what I need
for i in list:
    i = MyClass(text)

Thanks to all of your help!

4
  • 2
    Could you clarify the exact output you hope to acheive? Because the following might be what you're looking for: [ MyClass( text ) for _ in list ] or something along those lines. Commented Feb 20, 2014 at 4:05
  • Similar to this question. stackoverflow.com/questions/21598872/… Commented Feb 20, 2014 at 5:02
  • @sean I will provide a senario, lets say the input is unexpected employees' names and for each employees I require to process the data. so for each employee I must create a class. Commented Feb 21, 2014 at 4:14
  • @Tanveer that answer you provided will create something like this in a list <__main__.getchild instance at 0x97b1cac> while I expect it to be each A or B that can be played around easily. Commented Feb 21, 2014 at 4:15

1 Answer 1

7

In general, you can't and shouldn't shove things into your namespace like that. It's better to store those instances in a dict or a list

Class MyClass:
     def __init__(self,x):
           self.x = x

lst  = ["A","B"]  # don't use list as an identifier

myclasses = {k: MyClass(text) for k in lst}

Now your instances are

myclasses['A'], myclasses['B'] etc.

If you really want to create a handful of variables in your namespace:

A, B = (MyClass(text) for x in range(2))

note that this means you need to be explicit. You can't get the A,B from a file or user input etc.

Don't be tempted to use exec to pull this off. It's probably the wrong way to go about solving your problem. Tell us why you think you need to do it instead.

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

4 Comments

thanks for your answer. your answer doesn't look to be working but I can also tell you why I need that and I got interested that such idea doesn't seem to be correct by experienced programmers like all of you who answered means there is another way to solve such problem and I am going to tell the problem. so what I am trying to do is to input unexpected information like "employee names" and for each one of them i need to do some analysis which requires me to create class for each one of them that is why I need to create class objects automatically as I don't expect fixed input names.
@jesy2013, that's precisely why you shouldn't store them in your local namespace. If a name conflicts with a keyword or a builtin or something else in your namespace, you will get interesting bugs. Most likely you should be using a dict with the keys being the employee names.
thanks then how do you suggest me to use dict in such case? if the input is only employee names then what and how should I assign keys for each employees?
@jesy2013, If the employee names are unique, you could just use them as the dictionary keys. Alternatively you can use a list and append each instance to that assuming that the employee name is store in the instance.

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.