I'm a little new to Python, and was hoping that someone might be able to help me with a problem I've got.
I have the need to instantiate multiple objects using the variables in a list. Let's say I have a list of 2 items, ["apple", "orange"] and I want to use theses as names for a instantiated class based on class Fruit, so that I end up with 2 objects, apple and orange which are of class Fruit.
I've tried doing this but I can't seem to figure out how to use the variable name as the name of the instantiated class. This code doesn't work, but might give you some idea as to what I'm trying to achieve:
class Fruit:
pass
my_list = ["apple", "orange"]
for item in my_list:
item = Fruit()
type(apple)
This doesn't work but I hope that this gives you an idea as to what I'm trying to achieve. I get this error:
NameError: name 'apple' is not defined
What I would like to see is:
>>> type(apple)
<type 'instance'>
Any pointers that anyone can offer would be most appreciated :)
Thanks!