Python Forum
Changing Variables within Variables?
Thread Rating:
  • 4 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Variables within Variables?
#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


Messages In This Thread
Changing Variables within Variables? - by dave925 - Apr-20-2017, 05:38 AM
RE: Changing Variables within Variables? - by volcano63 - Apr-20-2017, 08:25 AM
RE: Changing Variables within Variables? - by Kebap - Apr-20-2017, 04:33 PM
RE: Changing Variables within Variables? - by wavic - Apr-21-2017, 08:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 340 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  importing variables from module 8376459 1 352 Feb-18-2024, 02:24 PM
Last Post: deanhystad
  Checking for Validity of Variables RockBlok 2 629 Dec-17-2023, 05:50 AM
Last Post: Gribouillis
  Create X Number of Variables and Assign Data RockBlok 8 1,158 Nov-14-2023, 08:46 AM
Last Post: perfringo
  mypy, type aliases and type variables tomciodev 1 815 Oct-18-2023, 10:08 AM
Last Post: Larz60+
  Unchangeable variables in a class? Calab 12 1,792 Sep-15-2023, 07:15 PM
Last Post: deanhystad
Question How create programmatically variables ? SpongeB0B 6 1,516 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,454 Aug-18-2023, 02:06 PM
Last Post: deanhystad
  Variables in a SCPI string thomaswfirth 3 1,059 May-22-2023, 05:04 PM
Last Post: thomaswfirth
  Advancing Through Variables In A List knight2000 0 562 May-13-2023, 03:30 AM
Last Post: knight2000

Forum Jump:

User Panel Messages

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