0

As it stands, I'm reading lines from a file to create "Monster" objects with certain int and string variables for each object. Every 2 lines creates a single object: one for strings, one for integers. Example file:

Slime,Slime ball
4,0,2,1,5,6,1
Pig,Pig skin
6,1,3,2,6,4,1

Here's my code to read from the file:

def read_from_file(file_name):
file = open(file_name)
monster_list = []
line_type = 1 #1=Monster Name, 2=Monster values
for each_line in file:
    if line_type == 1:
        #initialize the monster name
        value_list = each_line.split(",")
        name, trophy = value_list #name = first element, trophy = 2nd element
        line_type = 2
    else:
        if line_type == 2:
            #read values for monster values
            value_list = each_line.split(",") #now ['4','5',...]
            value_list = [int(e) for e in value_list] #now [1,2,3...]
            hp,shield,ap,mod,exp,gold,trophy_count = value_list
            line_type = 1
        monster_list.append(Monster(name, hp, shield, ap, mod, exp, gold, trophy, trophy_count))
#monster(monster_name)
file.close() #Close the file
return monster_list

Is there a way to alter the monster_list.append(...) line to use the name variable to instantiate a Monster object of the name? I.e.,

name = Monster(name, hp, shield, ap, mod, exp, gold, trophy, trophy_count)
monster_list.append(name)

Is this possible, or is there a similar way to accomplish this?

1 Answer 1

1

As far as I know, there is no way to do this. The reason being (again as far as I know) that there is no practical use for this.

Might I inquire what you would like to accomplish by doing this? Even if it would be possible, I have a feeling there are easier ways to get the result you are trying to achieve...

EDIT:

From your comment I would say you should be using a dictionary instead of a list. This would allow you to do the following:

monster_dict[name] = Monster(name, hp, shield, ap, mod, exp, gold, trophy, trophy_count))

Subsequently, in the function that calls 'read_from_file', you will be able to retrieve each monster by it's name from the dict:

slime = monster_dict['Slime']
Sign up to request clarification or add additional context in comments.

7 Comments

I'm all for an easier way. The idea here is to return a list, monster_list, full of Monster objects. At the moment, I have to access these monster objects by using monster_list[0] etc in the function that calls read_from_file(). I would like to be able to instantiate these as Slime = monster_list[0] but use the name, i.e, monster_list[0].name = monster_list[0].
I edited my answer. Would this work for what you are trying to do?
Possibly... I don't fully understand dictionaries, but if I call monster_dict['Pig'] - what happens? Does that find 'Pig' in the dictionary and finds the reference is an appropriate Monster object? EDIT: Oh, I think I see actually. But how do I return multiple dictionaries? monster_dict['Slime'] and monster_dict['Pig'] ?
Yes, thats basically what happens. You can read more about dictionaries here: docs.python.org/2/tutorial/datastructures.html#dictionaries I would definitely suggest reading about and understanding dictionaries, as they are one of the most fundamental and useful data structures python has to offer! :)
Thank you, that should work! Dictionaries also look rather helpful. It's about time I actually learn to use them! Thanks again.
|

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.