Python Performance: Why 'if not list' is 2x Faster Than Using len()
gigachad @ gigachad @sh.itjust.works Posts 6Comments 576Joined 2 yr. ago
gigachad @ gigachad @sh.itjust.works
Posts
6
Comments
576
Joined
2 yr. ago
Deleted
Permanently Deleted
Deleted
Permanently Deleted
But
None
has nolen
-> foo could be an empty list or
None
, it is ambiguous.len(foo)
will lead to an exceptionTypeError
iffoo
isNone
, I can cleanly catch that.It suggests I deal with a boolean when that is not the case. Explicit is better than implicit, and
if not foo
to check for an empty list may be pythonic, but it's still implicit af