As part of a bigger project in python3, I need a method in the spirit of to the following (not working) code:
class Qclass:
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def increment(Q, var):
eval('Q.{} = Q.{} + 100'.format(var,var))
Q = Qclass(1,2,3)
increment(Q,'a')
print(Q.a)
>>> 101
I need this because I don't have prior knowledge on which attribute of Q has to be incremented. A solution would be to declare specific functions for each attribute and use some selector, but I'd prefer to avoid this if possible.
Is there a tidy way to do this ?