3 ms·
Especially taking into account the more bizarre bugs (features?) of python: (bool(datetime.time(0)), bool(datetime.time(1))) == (False, True) I always con
by tomp 12y ago
Especially 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"); }