is_empty()
Check if an object is empty.
Implementation
Args: obj: The object to check Returns: True if obj is empty, False otherwise
Example
is_empty([])
Expected output: True
Alternative usage:
is_empty({})
Expected output: True
Source Code
def is_empty(obj: Any) -> bool:
# any non-collection primitive (no __len__ and no __iter__) is "empty"
if not hasattr(obj, "__len__") and not hasattr(obj, "__iter__"):
return True
if hasattr(obj, "__len__"):
return len(obj) == 0
# objects with __iter__ but no __len__ (e.g. generators)
return not any(True for _ in obj)