python - getattr in current function -
python - getattr in current function -
is there way variable of name
in below?
def save_info(self): name = 'hello' var1 = 'other item' var2 = 1 ... tv_series_fields_to_update = ['name', 'var2', 'var3', 'var4', ...] field in tv_series_fields_to_update: current_value = getattr_or_none(obj, field) new_value = # how variable of `name` ("hello") ?
given string of variable name, how value within current function?
use locals()
:
def save_info(): name = 'hello' var1 = 'other item' var2 = 1 tv_series_fields_to_update = ['name', 'var1', 'var2'] field in tv_series_fields_to_update: print field, locals()[field] save_info()
however, i'd urge consider using explicit dictionary instead of having bunch of unrelated variables.
python
Comments
Post a Comment