3 ms·
Mmm... you should always use 'if x is not None:' imo. It's very common for libraries to make values evaluate to False, and very easy to get bugs if you just la
by shadowmint 12y ago
Mmm... you should always use 'if x is not None:' imo.
It's very common for libraries to make values evaluate to False, and very easy to get bugs if you just lazily test with 'if x'.
Sqlalchemy springs to mind immediately as one of the common ones where using any() and if x: is a reeeeeallly bad idea; there are plenty of others.
I'm pretty skpetical about modifying your coding behavior based on what libraries you happen to be currently using.
'If x' isn't your friend.
- tomp 12y agoEspecially taking into account the more bizarre bugs (features?) of python: (bool(datetime.time(0)), bool(datetime.time(1))) == (False, True) I always consider `if x:` a bug, unless x can only be a boolean. Furthermore, it seriously hinders readability and clarity of the code.
- CmdrKrool 12y agoAgreed. Especially if you program in more than one language, trying to remember the subtleties of each one's collection of rules for "truthiness" is a fraught exercise. And isn't it Python people who like to say, "explicit is better than implicit"?
- Zancarius 12y agoI was specifically looking through these comments for your example. I got bit by this once, and it's certainly... strange (it's also surprising). It's considered "behavior consistent with the rest of Python" [1] (which I can agree with) even if it makes little sense in terms of immediate readability to someone who hasn't previously encountered it. Fortunately, the workaround is easy, and it is documented. There's at least a couple of spats on the mailing list regarding this feature that are of interest to the curious or at least those who are interested in the history of such behavior. [1] http://bugs.python.org/issue13936 http://bugs.python.org/issue13936
- rmrfrmrf 12y ago> I always consider `if x:` a bug, unless x can only be a boolean. Oh, like this? ;) var eventA = new Date(), eventB = new Date(); if (!parseInt((eventA - eventB) / 1000)) { console.log("these events occurred simultaneously"); } else { // troll harder with confusing use of 'asynchronous' console.log("these events occurred asynchronously"); }
- LyndsySimon 12y agoIt depends. If you're checking to see if that value is None, then yes - you should check that. If you're merely checking if the value is truthy, then using "if x:" is completely legitimate.
- pyfish 12y agoI agree. The author brings this to light as well and warns about the implications of changing what is considered "truthy" in x.
- keeperofdakeys 12y agoI've found "if x" to be much less readable, especially when I'm looking at code written in a language I'm not familiar with. When I'm reading such code, I want to be 100% what something is doing, and not have to read documentation when avoidable.
- glimcat 12y ago"Explicit is better than implicit." If you mean is not None, you should say is not None. It's fast and readable and there are no "just be aware that" disclaimers to tack on afterwards.