3 ms·
foo = 'lolsomestring' if foo: [code] > where a string is implicitly converted to a boolean (as opposed to using bool()). Umm, no. You're assuming tha
by anamax 16y ago
foo = 'lolsomestring'
if foo:
[code]
> where a string is implicitly converted to a boolean (as opposed to using bool()).
Umm, no. You're assuming that the condition of "if" is necessarily a boolean.
It's not clear that it is. It looks more like "is the value not an element of [None, 0, 0.0, False, {objects of 0 length}]" and a couple of others. (There's a strange exception for certain objects.)
The fact that False and True are Python constants of a "boolean type" does not imply that booleans are privileged wrt conditionals.
- xiongchiamiov 16y agoYou're assuming that the condition of "if" is necessarily a boolean. Indeed. Conditional branching generally needs a boolean value - do we go down this path or not? It's not clear that it is. It looks more like "is the value not an element of [None, 0, 0.0, False, {objects of 0 length}]" and a couple of others. Right, and any of those things, when converted to a boolean are... False. The things that don't convert to True. I don't know how the language itself is constructed, but it would surprise me if the code for bool() and if-statements were logically separated. Can you imagine the havoc that would ensue from, say, bool(None) returning False, but if None: causing the branch to execute?