Python Performance: Why 'if not list' is 2x Faster Than Using len()
Python Performance: Why 'if not list' is 2x Faster Than Using len()
None here
But None has no len
-> foo could be an empty list or None, it is ambiguous.
len(foo) will lead to an exception TypeError, I can cleanly catch that.
It suggests I deal with a boolean when that is not the case. Explicit is better than inplicit, and if not foo to check for an empty list may be pythonic, but it’s still implicit af
I am not saying it’s better, just that I don’t like the proposed way :) I would argue that being “pythonic” has even less value than the Zen, which I quoted because it’s true, not because it is some strict rule (which it isn’t anyway).
You could argue I also need to write that extra code for the if not case, as I explicitly have to check if it is None if my program somewhere further down expects only lists.
Hunting for those sweet milliseconds is a popular game in the Python community ;) if this mechanism is that important for your program, you should definitely use it, I would do as well!
Exactly. The point of following a code style is to make obvious patterns easy to spot and deviations stand out. That’s why code style guidelines say your priorities should be:
3 should only be prioritized if the win is big enough, and there should probably be a comment right there explaining why the deviation was made.