I have an attribute of Class named tag that is a list and I have to append to this list. The attribute name and value are both str variables. I can try
setattr(obj, tag, getattr(obj,tag).append(text))
but this creates unnecessary overhead, and also returns None. Currently I am using exec to achieve the same thing. Please help me find a better way, as exec is creating some trouble with the data.
exec(f"obj.{tag}.append({text})")
EDIT: Solved as per @Anentropic's comment
If you are mutating a mutable object, you can simply get the object with
getattrand do the mutating operation.But if you are trying to mutate an immutable property of an object you should do this: