0

EDIT: This is not a duplicate question. The answer received here actually creates new variables/objects inside the program. That this may or may not be what is actually best for me is immaterial. The 'duplicate' that was suggested does not actually create a new 'object'/'variable', it merely manipulates the powers of the dictionary.
This is not a duplicate answer.

I want to have user generated variables in a Python program.

For example, I would like to have a

raw_input('Enter new variable: ')

And perhaps the person wants to call his new variable 'X' and perhaps also set it equal to an integer 0.

Then, if it suits him, maybe create another variable 'Y' with a raw_input()...and Z, or A B C PHI OMEGA...so on as long as he likes. ... respectively setting them to, say, all integers.

This seems like a job for a list. Is it only a job for a list and appending, and calling from the list?

Remember I want to name/create a new variable ex nihilo. I am not asking the dull question of how to set an integer to a variable with raw_input() or anything else.

If you ask why, it is because I have a naive suspicion that this would be faster in the long run.

3
  • 5
    If you ask why, it is because I have a naive suspicion that this would be faster in the long run. It won't be. Use a dictionary. Commented Sep 30, 2013 at 17:12
  • How do you expect your program, written before the user creates these variables, to use them? Commented Sep 30, 2013 at 17:19
  • I'll have to think on that chepner, but at last I finally got the real answer to this question, not "the better answer to my problem." Commented Sep 30, 2013 at 17:36

2 Answers 2

6

In this case, using a dictionary will be a good idea:

varis = {} # a dictionary of variables, just name/value pairs
v = raw_input('Enter new variable: ')

varis[v] = "some value" # "create" a variable with the name stored in `v`

varis[v]                # "access" the variable's value
=> "some value"
Sign up to request clarification or add additional context in comments.

4 Comments

Perhaps I wasn't clear enough. It seems that, if this were looped, then entering a new value into v would destroy the old value. I do not want to destroy the old value/variable.
No, @user2179612, the data would persist in the varis dictionary. If the user first entered "john" and later "mary", then varis["john"] would continue to exist even after varis["mary"] was created.
Hmm ok. Well Rob gets the check mark because he gave me the technically correct answer to my question, although this solves the problem too. At the end of the day, "John" is still a string.
@Mr.A fair enough, but be aware that using exec() (or its close cousin, eval()) in general is a very bad programming practice, whenever that code is evaluated dynamically there exists the risk that someone injects malicious code. See this post, all the arguments against using eval() also apply to exec(). Truth be told, my solution is the preferred way to solve this kind of problem.
0

1) What you ask for is a bad idea. You almost certainly don't want to do it.

2) Here is how to do it:

name = raw_input("Name the variable:")
value = raw_input("And the value?")
exec("{} = {}".format(name, value))

1 Comment

Hey thanks perfect. I do now see one problem with this at least, and that is pre setting what I would do with these variables i do not yet know. Why wouldn't you do this? The only other way I saw is just using append for a list, but I've always read that is slow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.