Subtract boolean from float in python -
this question has answer here:
anyways when debuging code found statement subtracted boolean
float
.
then tried following in python console:
>>> 15.0 - true 14.0 >>> 15.0 - false 15.0
can explain me:
- why subtracting booleans numeric types legal (the docs state can
and
,not
,or
on boolean values: http://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not) - has practical use?
http://docs.python.org/3/library/stdtypes.html#boolean-values
boolean values 2 constant objects false , true. used represent truth values (although other values can considered false or true).
in numeric contexts (for example when used argument arithmetic operator), behave integers 0 , 1, respectively.
not nowadays, can write
result = numeric_value * a_bool
(this 1 used lot, example, in shader languages)instead of
result = numeric_value if a_bool else 0
or
result = (value_if_false, value_if_true)[a_bool]
don't of though.
it's people experience in lower-level languages expect, why take away them? in c, true , false still macros 1 , 0.
before 2.3, there no bool
type in python well, when introduced making subclass of int
made sure no code broken.
Comments
Post a Comment