Ultimately, what you want to do is incompatible with how Python is structured. You have the most elegant way to do this already if your variables are lists, but this is not possible with numbers.
This is because variables do not exist in Python. Links do. So ix not a list, it is a link to a list. Similarly, if it refers to a number. Therefore, if ix refers to y , then ix = z does not actually change the value of y , it changes the location in memory that ix points to.
In most cases, variables are treated as fields containing a value. The name is on the box. In python, values ββare fundamental, and "variables" are just tags that hang by a specific value. It is very nice when you get used to it.
In the case of a list, you can use the slice assignment, as you already do. This will allow all links to the list to see the changes, since you yourself change the list object. In the case of a number, there is no way to do this because numbers are immutable objects in Python. It makes sense. Five to five, and little can be done to change it. If you know or can define an attribute name, you can use setattr to change it, but that will not change other links that may already exist.
As Rafe Kettler says, if you can be more specific about what you really want to do, then we can come up with a simple elegant way to do this.
source share