Comment on Python Performance: Why 'if not list' is 2x Faster Than Using len()
iknowitwheniseeit@lemmynsfw.com 3 days agoYou’d need to explicitly check for None if using the len() construct as well, so this doesn’t change the point of the article.
gigachad@sh.itjust.works 3 days ago
But
None
has nolen
if not foo:
-> foo could be an empty list or
None
, it is ambiguous.len(foo)
will lead to an exceptionTypeError
, 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 afsugar_in_your_tea@sh.itjust.works 3 days ago
I don’t see it that way. If you’re doing
if len(foo) == 0
, you’re implying thatfoo
is expected to not beNone
, and expecting an exception should not be the default assumption, because exceptions should be… exceptional.Here’s what I assume:
if foo is not None
- empty values are explicitly acceptableif not foo
- the difference between an empty andNone
value isn’t importantif len(foo) == 0
- implicit assumption thatfoo
is notNone
(I frequently forget thatlen(…)
raises onNone
)If an exception was intended by the last bullet point, I prefer an explicit raise:
I actually use schema validation to enforce this at the edge so the rest of my code can make reasonable assumptions, and I’m explicit about whether each field may or may not be
None
.iknowitwheniseeit@lemmynsfw.com 3 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:
For the explicit length check:
Honestly you’re probably better off using type hints and catching such things with static checks and not adding the
None
check.gigachad@sh.itjust.works 3 days ago
This is what I would come up with:
There is no need to add a
None
check, asfoo
beingNone
should be considered as a faulty input. Avoiding the possibility offoo
beingNone
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 :)sugar_in_your_tea@sh.itjust.works 3 days ago
That’s terrible, and I would block that PR in a heartbeat, unless there was a very good reason for it (given context). I would instead prefer:
Exceptions are useful for bubbling up errors, they’re a massive code smell if you’re catching something thrown by local logic. Just like you shouldn’t catch
IndexError
right after indexing a list, you shouldn’t catchTypeError
right after checking the length. If you need to check parameters, check them at the start of your function and return early.mint_tamas@lemmy.world 3 days ago
Apart from the quote from the zen of python, does this really make your code better though? You will end up writing 4-5 lines with an extra level of indentation. The code does the same, but has worse performance and communicates the intent poorly (compared to the “pythonic” version).
gigachad@sh.itjust.works 3 days ago
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 isNone
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!
mint_tamas@lemmy.world 3 days ago
I think pythonic is more important than performance and I would still choose that version over a try-catch block, were it slower. Being pythonic means it represents a commonly understood pattern in Python code, therefore it is more efficient in communicating intent.