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

<- View Parent
iknowitwheniseeit@lemmynsfw.com ⁨6⁩ ⁨days⁩ ago

My point is that if your variable can be None then you need the same pattern for the length check.

So for the Pythonic version:

if (foo is not None) and not foo:
   ...

For the explicit length check:

if (foo is not None) and (len(foo) == 0):
  ...

Honestly you’re probably better off using type hints and catching such things with static checks and not adding the None check.

source
Sort:hotnewtop