input: Write a function that takes a list of objects and returns a list of those objects sorted by one of their attributes. If the attribute does not exist in the object, the function should return an empty list.
  
  def sort_by_attr(objects, attr):
      return sorted(objects, key=lambda item: item.get(attr, None)) if all(attr in obj for obj in objects) else []
  
  assert sort_by_attr([{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}], "age")==[{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}]
  assert sort_by_attr([{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}], "name")==[{"name": "Alice", "age": 20}, {"name": "Bob", "age": 21}]
  assert sort_by_attr([{"name": "Alice", "age": 20}, {"name": "Bob", "other_attr": 21}], "age")==[{"name": "Alice", "age": 20}]
  assert sort_by_attr([{"name": "Alice", "age": 20}, {"name": "Bob", "other_attr": 21}], "non_existent_attr")==[]
  assert sort_by_attr([], "age")==[]
output: def sort_by_attr(objects, attr):
      return sorted(objects, key=lambda item: item.get(attr, None)) if all(attr in obj for obj in objects) else []