Temporarily assign obj.attr to value.

def temporary_assignment(obj, attr, value):
  """Temporarily assign obj.attr to value."""
  original = getattr(obj, attr, None)
  setattr(obj, attr, value)
  yield
  setattr(obj, attr, original)