-> foo could be an empty list or None, it is ambiguous.
len(foo) will lead to an exception TypeError if foo is None, I can cleanly catch that.
It suggests I deal with a boolean when that is not the case. Explicit is better than implicit, and if not foo to check for an empty list may be pythonic, but it's still implicit af
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).
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!
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.
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:
follow whatever style the code around it uses
follow project style guidelines
do the technically optimal option
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.
I don't like it very much, my variable could also be
None
hereYou'd need to explicitly check for None if using the len() construct as well, so this doesn't change the point of the article.
But
None
has nolen
-> foo could be an empty list or
None
, it is ambiguous.len(foo)
will lead to an exceptionTypeError
iffoo
isNone
, I can cleanly catch that.It suggests I deal with a boolean when that is not the case. Explicit is better than implicit, and
if not foo
to check for an empty list may be pythonic, but it's still implicit afApart 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).
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!
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.
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.