@contextlib.contextmanager
def TempChangeField(obj, field_name, new_value):
 old_value = getattr(obj, field_name)
 setattr(obj, field_name, new_value)
 (yield old_value)
 setattr(obj, field_name, old_value)

@ contextlib . contextmanager def TempChangeField ( obj , field_name , new_value ) : old_value = getattr ( obj , field_name ) setattr ( obj , field_name , new_value ) ( yield old_value ) setattr ( obj , field_name , old_value )
context manager to change a field value on an object temporarily .