0

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)
8
  • 1
    What's the problem? You mean you want to have more than 3 instances? Commented Dec 13, 2016 at 10:37
  • You solved your own problem. What do you want to do ? Commented 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. Commented 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. Commented 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. Commented Dec 13, 2016 at 10:39

3 Answers 3

2

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>}
Sign up to request clarification or add additional context in comments.

Comments

1

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

That's hardcoding the instances... they say the number of instances is unknown.
Ah, so a dictionary can also store class?
It works! I think the dictionary solution is the way to go, thanks a lot!
Pouria: We don't know whether the total number of instances is know at creation time. So I provided examples how to add instances to a list or dict. If the OP understands this s/he can use those principles in list/dict comprehension. SamTulster: Yes you can reference any variable in a list or dictionary. (Under certain circumstances you can even use them as a key.)
That is hardcoding materials - not a valid solution in programming. Also, dictionary keys must be immutable. You cannot use mutable objects as dictionary key.
|
1

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}

Comments

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.