5 ms·
Seems fine just so long as (False + False) doesn't equal True.
by RodgerTheGreat 16y ago
Seems fine just so long as (False + False) doesn't equal True.
- cma 16y agoWhen is it ever desirable? I'm open to it having a use, but I don't see one. The only thing I've gotten from it is mismatched types that made it past some unit tests but blew up in the real world.
- RodgerTheGreat 16y agoI was making a glib joke, but to answer your question this is the tradeoff of using a not-so-strongly-typed language. Weakly-typed languages often save you some typing (if you'll forgive the pun) and implicitly convert in ways that can make it difficult to prevent those mistakes. It's just like Duck Typing- you're trading the need to explicitly specify an interface for the potential dangers of letting your interpreter "know what you mean". If this is frequently a problem, well... there are a lot of strongly-typed languages out there waiting for you to give them a try.
- eru 16y agoPython is fairly strongly typed in general. (At least compared to Perl and the like. Examples like True + True notwithstanding.) It is just also very dynamic.
- masklinn 16y ago> I was making a glib joke, but to answer your question this is the tradeoff of using a not-so-strongly-typed language. Not in this case. In this case it results from: 1. Python historically not having booleans and using 0 and 1 instead 2. As a result, `bool` inherits from `int` for compatibility reasons >>> issubclass(bool, int) True with False mapping to 0 and True to 1 for compatibility purposes >>> int(False), int(True) (0, 1) 3. Add isn't overridden on bool >>> bool.__add__ <slot wrapper '__add__' of 'int' objects> therefore, when adding two booleans you're really adding two integers. Most non-strongly-typed languages will not have the following behavior >>> "foo" + 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'int' objects
- masklinn 16y agoIn fact, to expand on the latter part of my comment, "weak typing" is generally understood as implying implicit type coercions or conversions at runtime. PHP and Javascript famously convert between numbers and strings depending on a bunch of factors (and usually the wrong way leading to unreliable results). In that sense, Python is a very strongly typed language: from the top of my head, the only example of implicit type conversion in Python is the promotion of `int` to `long`, which has been removed from 3.x (all integer types were unified). All other conversions have to be explicit as far as I remember. That makes Python more strongly (but less statically) typed than Java (see the very strange overloading of the `+` operator when any operand is a string, as well as autoboxing since 1.5) and C# (C# allows for implicit type conversions, including in userland, and the cast operator doubles up as an explicit type conversion operator which I find terrible)
- jakevoytko 16y agoI used it in Code Golf to select a string from a list based on the truth of some conditions. So no, it does not have a use :)
- bobbyi 16y agoIt's useful because it allows you to add together booleans to get a count of how many are true. You can check if two out of three are true with (bool1 + bool2 + bool3) >= 2 You can do count_negatives = sum(number < 0 for number in numbers)
- oddthink 16y agoThis is only tangentially related, but try looking up "Iverson brackets" and Knuth's "Two notes on notation". One argument there is that False - True = -1 makes sign(x) = [x > 0] - [x < 0], which is convenient. I don't know of a case where True + True = 2 helps, but maybe something involving dual sums would be relevant.
- cma 16y agoThe Iverson Bracket would already exist in a python that didn't subclass bool from int; '[boolean_expression]' would be, in python notation: 'int(boolean_expression)'. Sounds kinda like I'm making the same argument as Knuth in forcing '[]' over '()'.
- eru 16y agoIn mathematics True + True == False might be a common interpretation.
- moron4hire 16y agoWhen? Because the + symbol is often used to represent boolean OR, so T + T == T.