0

Is it possible to store a variable inside another variable?

x = 5
a = x

This would return 5 if I were to print(a) it.

I want to make it so that when I say (after a = x) a = 4 I would actually be changing the value of x .

For context, I am selecting a random variable from an existing list, checking to see if it isn't 0, then changing it to 2. If it is zero, I want to select a new variable and repeat.

Thank you, all help is appreciated.

nat-echlin
  • 63
  • 5
  • You can do that, sorta, but i don't think it's going to go how you want. – SuperStew Dec 12 '17 at 19:57
  • 1
    You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Dec 12 '17 at 19:57
  • No, python variables do not work like this. You can use some sort of wrapper class to simulate that. However, given your problem description, it sounds neither necessary nor desirable to do it that way... – juanpa.arrivillaga Dec 12 '17 at 19:57
  • If you want to choose a value from a list, you can use the index into the list as a reference to a particular value. – Blorgbeard Dec 12 '17 at 19:58
  • 2
    I suggest you post the code that you're working with and explain specifically what you're trying to do with reference to it. This appears to be a classic [XY problem](http://xyproblem.info/) at the moment. – Blorgbeard Dec 12 '17 at 19:59
  • You _could_ do `x=[5]; a=x; a[0]=4` and then `x[0]` will also be 4 because there's actually only one list object there with two names: `x` and `a`. But see what happens if you then do `a = [3]; print(x)`. If you find that result puzzling, you _need_ to read the article I linked. – PM 2Ring Dec 12 '17 at 20:08

1 Answers1

-4

If you were to initialise a, then set x equal to a instead, whatever value you change a to x will become:

a = 5
x = a
a = 4

print(x) = 5

x = a

print(x) = 4

Must be after you set the value

lcodegmaster
  • 74
  • 1
  • 6
  • 3
    Did you test this? I suggest you do that ASAP before you get some very buggy code. – roganjosh Dec 12 '17 at 20:01
  • It doesn't work because `int`s are immutable. –  Dec 12 '17 at 20:02
  • 1
    @Tobias the mutability or immutability of `int` objects is entirely irrelevant. – juanpa.arrivillaga Dec 12 '17 at 20:05
  • 1
    I suggest you also should read _Facts and myths about Python names and values_ – PM 2Ring Dec 12 '17 at 20:09
  • @juanpa.arrivillaga Explain to me how it is completely irrelevant. All operations on immutable values return a new value, for example `a = 5; x = a; a -= 1`. If `int`s were mutable, then both would be `4`. –  Dec 12 '17 at 20:11
  • @Tobias that is not the same as the code in this answer. Try the code above with a `list` (mutable). `a = [1,2,3]; x = a; a = [3,2,1];` Same result, x is not affected, because `=` doesn't work that way, whether the operand is mutable or not. – Blorgbeard Dec 12 '17 at 20:14
  • No, *assignment never works that way, regardless of the object type* You can go ahead and try it with `list` objects if you want, i.e. replace `a = ['foo']` the `b = ['bar']` then `a = b`, then `a = ['baz']` and see `b` is still `['bar']` – juanpa.arrivillaga Dec 12 '17 at 20:15
  • @roganjosh I think this is just a poorly communicated post. All this is saying is that you have to do `b = a` *after* the assignment, which is true – juanpa.arrivillaga Dec 12 '17 at 20:16
  • @juanpa.arrivillaga see the initial post. The edit came after my comment and I think it actually conveys confusion on the part of the poster after they realised the code they posted didn't work the way they expected. – roganjosh Dec 12 '17 at 20:17
  • 1
    @Tobias Also see my example [above](https://stackoverflow.com/questions/47780550/can-i-store-a-variable-inside-another-variable#comment82522756_47780550). – PM 2Ring Dec 12 '17 at 20:26
  • I addition to the suggestion from PM 2Ring, another article I often link to is https://jeffknupp.com/blog/2012/11/13/is-python-callbyvalue-or-callbyreference-neither/ for understanding what's going on here. Also note that you can delete poorly-received answers and it should give you the reputation back if you wish. – roganjosh Dec 12 '17 at 20:26