Python Forum

Full Version: Changing Variables within Variables?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How do I make variables within variables modifiable?

y = '1'
x = 'hello'+y
y = '2'

If I print <x>, it will only give me  'hello1' not 'hello2'. 

Is there an easy way to make <y> modifiable within <x>?
That would go very much against all (or most) principles of programming.
A workaround that first comes to mind is creating an object, and have your variables be its member variables.
(Apr-20-2017, 05:38 AM)dave925 Wrote: [ -> ]How do I make variables within variables modifiable?

y = '1'
x = 'hello'+y
y = '2'
If I print <x>, it will only give me  'hello1' not 'hello2'. 

Is there an easy way to make <y> modifiable within <x>?

You are confusing string concatenation with list copy modification
When you do something like 
x = [1, 2]
y = x
x[0] = 2
then y[0] will become 2 too, because both x and y point to the same list in memory. Assignment in Python does create a new alias (reference, pointer) to right hand operator.

With x = 'hello'+y you create a new temporary string object than ends with value of the string in y and creates reference x to that object; that new object does not contain reference to y

Of course, you can always try  to override string object to simulate that behavior Naughty
Make it a dictionary,that's what python dos internally anyway.
>>> y = '1'
>>> x = 'hello'+y
# Stored as dictionary,but x has now reference to y
>>> globals()
{'y': '1',
'x': 'hello1'}
As mention bye volcano63 so get reference to y lost when concatenate with "hello".
So a dictionary that is visible.
>>> y = '1'
>>> d = {'hello': y}
>>> d
{'hello': '1'}
>>> vaule = ''.join(d.items()[0])
>>> vaule
'hello1'

>>> y = '2'
>>> d['hello'] = y
>>> d
{'hello': '2'}
>>> new_vaule = ''.join(d.items()[0])
>>> new_vaule
'hello2'
(Apr-20-2017, 12:40 PM)snippsat Wrote: [ -> ]Make it a dictionary,that's what python dos internally anyway.

Then collections.OrderedDict, otherwise the order is not guaranteed, and join may yield 1hello instead of hello1
The key:value will never change.
I know what you mean a dictionary with more key:value can order change.

So here order change,not key:value
# 3.4
>>> d
{'hello': '1'}
>>> d1 = {'car': 5}
>>> d.update(d1)
>>> d
{'car': 5, 'hello': '1'}
Using 3.4 over,in 3.6 dict is ordered(not guaranteed yet).
# 3.6
>>> d = {'hello': 1}
>>> d1 = {'car': 5}
>>> d.update(d1)
>>> d
{'hello': 1, 'car': 5}
(Apr-20-2017, 05:38 AM)dave925 Wrote: [ -> ]How do I make variables within variables modifiable?

y = '1'
x = 'hello'+y
y = '2'

If I print <x>, it will only give me  'hello1' not 'hello2'. 

Is there an easy way to make <y> modifiable within <x>?

Why do you want to do this? What is your end goal here?
Thanks for all your replies. To answer what I was doing that led to the question:

I started my program declaring my variables.

day, date, topic = '','',''


then I declared my title using the above variables....

title = day+date+topic

then, I started my program with a loop which altered the day, date, and topic, but I noticed that when I wanted to write the title, it was just blank because the variables weren't fluid within my variable declaration. I suppose, like you all are saying, I would need to declare my title as a List or Dictionary to make the variables modifiable.

Thanks again!
You are missing something.
When you do
y = '1'
x = 'hello'+y
y = '2'
the result of the 'hello' + y is stored, not the expression itself. And x is a pointer to the address in the memory.
(Apr-21-2017, 02:14 AM)dave925 Wrote: [ -> ]Thanks for all your replies. To answer what I was doing that led to the question:

I started my program declaring my variables.

day, date, topic = '','',''


then I declared my title using the above variables....

title = day+date+topic

then, I started my program with a loop which altered the day, date, and topic, but I noticed that when I wanted to write the title, it was just blank because the variables weren't fluid within my variable declaration. I suppose, like you all are saying, I would need to declare my title as a List or Dictionary to make the variables modifiable.

Thanks again!

If you don't want to write title = day+date+topic inside whatever loop you're using, then you could write a small function like:

def get_title(day, date, topic):
    return "{0}{1}{2}".format(day, date, topic)
...and just use the function instead.  But having a string who's contents change depending on where you are in your program, is a pathway that leads directly to madness.