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.

Mr. A
  • 219
  • 4
  • 11
  • 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. – David Robinson Sep 30 '13 at 17:12
  • How do you expect your program, written before the user creates these variables, to use them? – chepner Sep 30 '13 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." – Mr. A Sep 30 '13 at 17:36

2 Answers2

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"
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 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. – Mr. A Sep 30 '13 at 17:13
  • 1
    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. – Robᵩ Sep 30 '13 at 17:21
  • 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 Sep 30 '13 at 17:33
  • @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](http://stackoverflow.com/a/1832957/201359), 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. – Óscar López Sep 30 '13 at 18:57
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))
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 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. – Mr. A Sep 30 '13 at 17:29