i would like to write a class that takes in a couple of variables in the form of (variableName, value). However variable names are only known at runtime and also the number of args are known at runtime. In my class, i have an object where i want to set those paramaters. So what i need to loop over all variables and setattribute as below.
setattr (self.obj, variableName, value)
self.obj.make()
You can do:
which you would call with
C(x=3, y=123,…).If you really want to use a [variableName, value,…] list of arguments, you can do:
which you call with
C('x', 3, 'y', 123,…). Thezip()trick groups arguments of the list of arguments (args) by pairs. You might want to add a check on the number of arguments passed (C(3)is accepted by the above code becausezip()does not mind using two sequences of different length).