Why? not x means x isNoneorlen(x) == 0 for lists. len(x) == 0 will raise an exception if x is None. In most cases, the distinction between None and [] isn’t important, and if it is, I’d expect separate checks for those (again, for explicitness) since you’d presumably handle each case differently.
In short:
if the distinction between None and [] is important, have separate checks
if not, not x should be your default, since that way it’s a common pattern for all types
Why?
not x
meansx is None or len(x) == 0
for lists.len(x) == 0
will raise an exception ifx
is None. In most cases, the distinction betweenNone
and[]
isn’t important, and if it is, I’d expect separate checks for those (again, for explicitness) since you’d presumably handle each case differently.In short:
None
and[]
is important, have separate checksnot x
should be your default, since that way it’s a common pattern for all types