I'm trying to create variable names such as:
event1
event2
event3
to name different instances of the same class.
I searched online, and a lot of people are suggesting dictionary. But as far as I know, dictionary only works for pairs. I want to name different instances of the same class, for example:
class Event:
def __init__(self, description):
self.__description = description
event1 = Event('This is event 1')
event2 = Event('This is event 2')
event3 = Event('This is event 3')
Does anyone know how I can do that? Thanks in advance.
Edit 1:
It appears my question was not clear enough. I do not know how many instances are needed. I will read the length of a file (the number of lines in the file), and then create instances of the class.
Edit 2:
I have multiple pieces of information to fill in the class. For example:
class Event:
def __init__(self, name, description, value):
self.__name = name
self.__description = description
self.__value = value
event1 = Event('This is event 1', 'Tom eats apple', '20')
event2 = Event('This is event 2', 'Jerry eats pie', '10')
#......
#(maybe 20 or more)
-
1What's the problem? You mean you want to have more than 3 instances?ettanany– ettanany2016年12月13日 10:37:16 +00:00Commented Dec 13, 2016 at 10:37
-
You solved your own problem. What do you want to do ?MMF– MMF2016年12月13日 10:38:30 +00:00Commented Dec 13, 2016 at 10:38
-
Yes, much more than 3 instances. The design is to read a file, find out how long the file yes, and create as many instances of the class as needed.SamTulster– SamTulster2016年12月13日 10:38:42 +00:00Commented Dec 13, 2016 at 10:38
-
Where does the value fed to each instance comes from? If it is a list, then you can just store your instances in a list.Pouria– Pouria2016年12月13日 10:38:44 +00:00Commented Dec 13, 2016 at 10:38
-
I do not know how many instances there will be. Perhaps 10, perhaps 100. I want to generate as many variable names as needed.SamTulster– SamTulster2016年12月13日 10:39:49 +00:00Commented Dec 13, 2016 at 10:39
3 Answers 3
To create many instances dynamically, one solution would be using a dictionary like below:
class Event:
def __init__(self, description):
self.__description = description
events = {}
num_events = 5 # Or whatever
for i in range(num_events):
events['event{}'.format(i + 1)] = Event('This is event {}'.format(i + 1))
Output:
>>> events
{'event4': <__main__.Event instance at 0x02F70E40>, 'event5': <__main__.Event instance at 0x02F70DF0>, 'event2': <__main__.Event instance at 0x02F70DA0>, 'event3': <__main__.Event
instance at 0x02F70D50>, 'event1': <__main__.Event instance at 0x02F70C60>}
Comments
Use a list:
class Event:
def __init__(self, description):
self.__description = description
event = []
event.append(Event('This is event 0'))
event.append(Event('This is event 1'))
event.append(Event('This is event 2'))
...
So you can store as many instances you want without having to know in advance how many.
The solution with dictionaries is similar:
event = {}
event[1] = Event('This is event 1')
event[2] = Event('This is event 2')
event[3] = Event('This is event 3')
...
or
event = {}
event['a'] = Event('This is event A')
event['b'] = Event('This is event B')
event['c'] = Event('This is event C')
...
Depends on how you want to identify them.
9 Comments
Here is a class:
class Event:
def __init__(self, description):
self.__description = description
now we want to create n instances of this class, using entries obtained from multiple lists:
entries_a = ['input 1', 'input 2', 'input 3']
entries_b = ['something 1', 'something 2', 'something 3']
entries_c = ['a', 'b', 'c']
Here is how we combine all the entries (the must be of the same length).
entries = zip(entries_a, entries_b, entries_c)
Here is how we can create instances of that class for the entries:
instances = [Event(val_a, val_b, val_c) for val_a, val_b, val_c in entries]
or using dictionary:
instances = {(val_a, val_b, val_c): Event(val_a, val_b, val_c) for item in entries}