Sir, I will make sure to never bother you with a PR and my terrible, terrible code ;)
gigachad
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, but I am also okay with being in the wrong here,
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!
But None
has no len
if not foo:
-> 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
I don't like it very much, my variable could also be None
here
Absolutely agree. I just use it for some simple stuff like "every nth row in a pandas dataframe slice a string from x to y if column z is True" or something like that. These logics take time to write, and GPT usually comes up with a right solution or one that doesn't need a lot of modification.
But debugging or analyzing an error? No thanks
Why is the European Space Agency speaking up to this topic
...as reported by the South China Morning Post lol
Maybe or maybe not. Being long-term invested (>15 years) will solve this issue for you. Studies suggest you probably realized a loss and will also miss the entry point when stocks are going up again. Remember folks, time in the market beats timing the market.
To be fair - this was a non-aggression pact, nothing about an alliance.