def METHOD_NAME(obj):
  if not C.active_object == obj:
    try:
      bpy.ops.object.mode_set(mode='OBJECT')
    except:
      pass
    bpy.ops.object.select_all(action='DESELECT')
    obj.select_set(True)
    C.view_layer.objects.active = obj
  bpy.ops.object.mode_set(mode='OBJECT')