Changing Variables within Variables? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Changing Variables within Variables? (/thread-2931.html) |
Changing Variables within Variables? - dave925 - Apr-20-2017 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>? RE: Changing Variables within Variables? - j.crater - Apr-20-2017 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. RE: Changing Variables within Variables? - volcano63 - Apr-20-2017 (Apr-20-2017, 05:38 AM)dave925 Wrote: How do I make variables within variables modifiable? You are confusing string concatenation with list copy modification When you do something like x = [1, 2] y = x x[0] = 2then 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 RE: Changing Variables within Variables? - snippsat - Apr-20-2017 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' RE: Changing Variables within Variables? - volcano63 - Apr-20-2017 (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
RE: Changing Variables within Variables? - snippsat - Apr-20-2017 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} RE: Changing Variables within Variables? - Kebap - Apr-20-2017 (Apr-20-2017, 05:38 AM)dave925 Wrote: How do I make variables within variables modifiable? Why do you want to do this? What is your end goal here? RE: Changing Variables within Variables? - dave925 - Apr-21-2017 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! RE: Changing Variables within Variables? - wavic - Apr-21-2017 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.
RE: Changing Variables within Variables? - nilamo - Apr-26-2017 (Apr-21-2017, 02:14 AM)dave925 Wrote: Thanks for all your replies. To answer what I was doing that led to the question: 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. |