Python Forum
Changing Variables within Variables?
Thread Rating:
  • 4 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Variables within Variables?
#1
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>?
Reply
#2
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.
Reply
#3
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
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'
Reply
#5
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
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}
Reply
#7
(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?
Reply
#8
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!
Reply
#9
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  importing variables from module 8376459 1 245 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  Checking for Validity of Variables RockBlok 2 489 Dec-17-2023, 05:50 AM
Last Post: Gribouillis
  Create X Number of Variables and Assign Data RockBlok 8 867 Nov-14-2023, 08:46 AM
Last Post: perfringo
  mypy, type aliases and type variables tomciodev 1 654 Oct-18-2023, 10:08 AM
Last Post: Larz60+
  Unchangeable variables in a class? Calab 12 1,402 Sep-15-2023, 07:15 PM
Last Post: deanhystad
Question How create programmatically variables ? SpongeB0B 6 1,215 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  How to store columns of a .csv in variables (when the .csv has no headers) ? hobbyist 6 1,146 Aug-18-2023, 02:06 PM
Last Post: deanhystad
  Variables in a SCPI string thomaswfirth 3 861 May-22-2023, 05:04 PM
Last Post: thomaswfirth
  Advancing Through Variables In A List knight2000 0 499 May-13-2023, 03:30 AM
Last Post: knight2000
  Trying to understand global variables 357mag 5 1,061 May-12-2023, 04:16 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020