1

I want to select a variable by using another variable. In C I would store the address of the selected variable in a pointer. If I now want to read the variable I use this pointer. But in python there are no pointers. So I did it by using eval. The set function is called first and after some time the get function is called. The get function should return the same as long as the selection stays the same.

See the example. Is there a better way?

sel = 0
a = 1
b = 2

def set(index):
    if index == 0:
        sel = 'a'
    elif index == 1:
        sel = 'b'
    return sel

def get(sel):
    global a, b
    return eval(sel)

sel = set(0)
print(get(sel))
a = 3
print(get(sel))
sel = set(1)
print(get(sel))

The result should be:

1
3
2
GePe
  • 11
  • 3
  • 3
    Yes, the better way is to start thinking in python and to stop thinking in c. – quamrana Jul 10 '22 at 17:32
  • 4
    Perhaps use a dict? BTW don't use `set` as a variable name, as it is a built-in function. Just a simple `{'do_a': 'a', 'do_b': 'b'}` would do in this case. For more complex problems, you can put functions or lambda expressions as the values of the dict. – j1-lee Jul 10 '22 at 17:32
  • 1
    This looks like the behavior of a regular array. Simply indexing the list [1, 2] would have the exact same effect? – nenad.popovic Jul 10 '22 at 17:34
  • 1
    It's not clear to me why `return('a')` is preferable to `return a`. What am I missing? – navneethc Jul 10 '22 at 17:35
  • If you really want to do it the way you describe, see [this question](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string). However, I share the sentiment that the more sensible approach would be a data structure like a dict or list. – bgfvdu3w Jul 10 '22 at 17:38
  • The set() function represents an external request with an integer as argument. This integer selects which variable I have to return at the next get() function(s). The value of the variable the get() function returns can be changed in between two get() calls. – GePe Jul 11 '22 at 15:55
  • A dict or a list will not work, because the values of the variables can be changed in between two calls to get(). Using this I always get the value set at the initialization of the dict. I think this is because integers are immutable. – GePe Jul 11 '22 at 16:05
  • Uups, now I saw that my code is wrong. It doesn't represent exactly what I want. As written above the set() function has an integer and should set the string to evaluate. – GePe Jul 11 '22 at 16:18
  • Uups, now I saw that my code is wrong. It doesn't represent what I want. As written above the set() function gets an integer and should set the string to evaluate. So it should return the variable to get (e.g. 'a'). The get() function uses eval(value_to_get) and returns the result. – GePe Jul 11 '22 at 16:27
  • Now I changed the code. – GePe Jul 11 '22 at 16:38
  • After some searching I found that using a lambda function also works. I think it is better to use a lambda without an argument for the job. So the change is sel = lambda: a or sel = lambda: b and in get I will return sel(). – GePe Jul 12 '22 at 06:17

0 Answers0