I just encountered the following and am curious about Python's behavior:
>>> x = 1
>>> x in range(2)
True
>>> type(x in range(2))
<type 'bool'>
>>> x in range(2) == True
False
>>> x in range(2) == False
False
>>> (x in range(2)) == True
True
In particular, why does (1 in range(2)) == True
evaluate True
and l in range(2) == True
evaluate to False
? It seems like there is some strange order of evaluation behavior in the latter, except that if you make the order explicitly wrong, you get a TypeError
:
>>> x in (range(2) == True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument of type 'bool' is not iterable
For the record, I don't know any cases where I would use x in range(2) == True
instead of just x in range (2)
, but would just like to know why this is happening. I also tested this in both Python2.7 and Python3, and the behavior is the same.
Boolean operations in Splunk have an order of evaluation similar to mathematical operations. In case of using expressions with the SEARCH command and expressions are within the parentheses, the order of evaluation must be NOT, OR, and AND.
A Boolean expression will be evaluated to either true or false if all of its variables have been substituted with their actual values. For example, a Boolean variable x is a Boolean expression, and it can be evaluated to true or false subject to its actual value.
In Java, the operands of an operator are always evaluated left-to-right.
The result of a Boolean expression is either true or false. Boolean expressions allow us to write programs that decide whether to execute some code or not. These decisions changes the flow of the program execution.
The below expression:
x in range(2) == True
is chained comparison, and is evaluated as:
x in range(2) and range(2) == True
which will give you False
as range(2) == True
is evaluated to False
. See documentation for Comparison:
Comparisons can be chained arbitrarily, e.g.,
x < y <= z
is equivalent tox < y and y <= z
, except that y is evaluated only once (but in both cases z is not evaluated at all whenx < y
is found to be false).
The ==
equality and in
membership operators are both comparison operators, and these can be chained.
Chaining takes the form of expr1 op1 expr2 op2 expr3
, which is interpreted as (expr1 op1 expr2) and (expr2 op2 expr3)
but the middle expr2
is only evaluated once.
So, your examples are really:
x in range(2) and range(2) == True
and range(2)
is never equal to a boolean value.
Note that you should really never compare to == True
or == False
. Leave that to while
or if
to test for you.
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