Elegant way to modify a list of variables by reference in Python? -
let's have function f() takes list , returns mutation of list. if want apply function 5 member variables in class instance (i), can this:
for x in [i.a, i.b, i.c, i.d, i.e]: x[:] = f(x)
1) there more elegant way? don't want f() modify passed list.
2) if variables hold simple integer (which won't work slice notation), there way? (f() take & return integer in case)
another solution, though it's not elegant:
for x in ['a', 'b', 'c', 'd', 'e']: setattr(i, x, f(getattr(i, x)))
Comments
Post a Comment