Comment on Python Performance: Why 'if not list' is 2x Faster Than Using len()

<- View Parent
sugar_in_your_tea@sh.itjust.works ⁨2⁩ ⁨days⁩ ago

Well yeah, because I’m explicitly not defining a difference between None and []. In most cases, the difference doesn’t matter.

If I did want to differentiate, I’d use another if block:

if foo is None:
    ...
if not foo:
    ...

Explicit is better than implicit. I hate relying on exceptions like len(foo) == 0 raising a TypeError because that’s very much not explicit.

Exceptions should be for exceptional cases, as in, things that aren’t expected. If it is expected, make an explicit check for it.

source
Sort:hotnewtop