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

<- View Parent
gigachad@sh.itjust.works ⁨6⁩ ⁨days⁩ ago

This is what I would come up with:

try:
    if len(foo) == 0:
    ...
except TypeError:
    ...

There is no need to add a None check, as foo being None should be considered as a faulty input. Avoiding the possibility of foo being None from the beginning using static checks or testing is of course the preferred solution. But in reality we do not work in such optimal environments, at least I can say that from the perspective of data science, where often procedural, untested code is produced that runs only a few times. But I get your point and I think both paths are viable, maybe we can agree on that :)

source
Sort:hotnewtop