So there's a few partial answers to this here, but nothing clearly answers this question.
I want to take a .txt file with an arbitrary number of names, each on a new line, like this.
John Citizen
Genghis Khan
Ex Parrot
... could be hundreds of names
Then I want to create a new instance of this Friend() object
class Friend(object):
def __init__(self, name):
self.name = name
self.somequality = "somequality"
def somemethod(self):
return self.somequality
for each line in the text file.
Currently I have this code:
friends_dict = {}
with open('NameList.txt') as f:
friend_list = [line.strip() for line in f]
friend_instance_list = []
for i in friend_list:
friend_instance_list.append(Friend(i))
friends_dict = dict(zip(friend_list, friend_instance_list))
And this works, it makes a dictionary of instances, paired with the strings they represent. I can even call their names like this:
print(friends_dict["John Citizen"].name)
Is this actually the best way to do things? It seems really clumsy and annoying not to have any of these objects assigned to variables. It makes dealing with them a real pain, but I don't have any idea how to go about assigning these instances from the dictionary to variables.