Anyways when debuging my code I found statement that basically subtracted boolean
from float
.
Then I tried following in python console:
>>> 15.0 - True
14.0
>>> 15.0 - False
15.0
Can anyone explain to me:
and
, not
and or
on boolean values: http://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not)It is legal, because bool
is a subclass of int
:
>>> bool.__bases__
(<type 'int'>,)
>>> True == 1
True
>>> False == 0
True
Yes, it has some practical application. For example it was possible to do something like that before ternary statement was introduced:
result = [value_if_false, value_if_true][condition]
Which basically does what would be done in this code:
if condition:
result = value_if_false
else:
result = value_if_true
Other practical usages are:
sum
several checks / booleans to receive number of results equaling True
,you can multiply by the result of the check:
result = can_count_a * a + can_count_b * b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With