i want to know if it is possible to handle variables through a dictionary.
Example:
global MyVar
...
MyVar = 0xFFFF
d={"MyVar":MyVar}
def ChangeVar (var_to_change, value)
d[var_to_change] = value #I know this code assigns a new value to the key...
...
ChangeVar("MyVar",25)
...
I want to use the dictionary to “select” which variable I will modify. I can always use an array, but I want to know if it is possible to accomplish this. I do not know very much about pointers in Python.
In C would be something like this (maybe I wrote an extra or missed an ‘*’):
int MyCVar = 0;
int MyCVar2 = 0;
int *MyD[] = {&MyCVar,&MyCVar2};
...
*(MyD[index]) = some_value;
I hope you understand what I am trying to explain.
Thank you,
Jonathan
EDIT:
Thank you all for your answers. What I was trying to do was kind of hard since python handles the names and objects quite different. What I did was something like this:
class mytype(object):
def __init__(self,name=""):
self.__Name = name
....
myDict[name] = self
then I can access all the objects using the Name of the variable. This is the behavior I tried to explain in the first example. And this is how I would access those objects.
myDict["MyVar"].Value = 1
Thank you all, your answers gave some good ideas.
In Python there is no sugar to get double-pointer semantics. You need to pass a reference some some object around that can mutate the value you want. For example your dictionary could store a function that had access to
MyVarand could change it.You’ll be better off designing this to pass around an object that wraps the actual value you care about.