2

So I just found out today that I can import variables from other Python files. For example, I can have a variable green = 1, and have a completely separate file use that variable. I've found that this is really helpful for functions.

So, here's my question. I'm sorry if the title didn't help very much, I wasn't entirely sure what this would be called. I want my program to ask the player what his or her name is. Once the player has answered, I want that variable to be stored in a separate Python file, "player_variables.py", so every time I want to say the player's name, instead of having to go to from game import name, I can use from player_variables import name, just to make it easier.

I fully understand that this is a lazy man's question, but I'm just trying to learn as much as I could. I'm still very new, and I'm sorry if this question is ridiculous. :). Thanks for the help, I appreciate it. (be nice to me!)

2
  • 2
    Can you post some code that you've tried and describe what isn't working? Commented Jun 9, 2014 at 1:21
  • It doesn't sound as if you are on the right track. This is data that should probably be in a database Commented Jun 9, 2014 at 1:24

2 Answers 2

1

From your question, I think you're confusing some ideas about variables and the values of variables.

1) Simply creating a python file with variable names allows you to access the values of those variables defined therein. Example:

# myvariables.py
name = 'Steve'

# main.py
import myvariables
print myvariables.name

Since this requires that the variables themselves be defined as global, this solution is not ideal. See this link for more: https://docs.python.org/2/reference/simple_stmts.html#global

2) However, as your question states that you want your program to save the variable of some user-entered value in the python file, that is another thing entirely as that's metaprogramming in python - as your program is outputting Python code, in this case a file with the same name as your variables python file.

From the above, if this is the means by which you're accessing and updating variables values, it is a simpler idea to have a config file which can be loaded or changed at whim. Metaprogramming is only necessary when you need it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your help. I'm surprised what I was trying to do could not be done, however. I will try to learn how to make a config file.
@KyleMe: You're welcome. But, you misunderstand me: it can be done - undoubtedly. But, as what you intend to do is actually more involved that what it should be, I proposed an alternative.
1

I think you should use a data format for storing data, not a data manipulating programming language for this:

import json
import myconfig

def load_data():
    with open(myconfig.config_loc, 'r') as json_data:
        return json.loads(json_data.read())

def save_data(name, value):
    existing_data = load_data()
    existing_data[name] = value
    with open(myconfig.config_loc, 'w') as w:
        w.write(json.dumps(existing_data, indent=4, sort_keys=True))

You should only store the location of this json file in your myvariables.py file, but I'd call it something else (in this case, myconfig, or just config).

More on the json library can be found here.

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.