Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of evaluation when testing equivalence of booleans

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.

like image 239
mdml Avatar asked Oct 22 '13 17:10

mdml


People also ask

What is the order of evaluation for Boolean operations?

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.

What are the rules for evaluating Boolean expressions?

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.

What is the order of the evaluation of the logical operators Java?

In Java, the operands of an operator are always evaluated left-to-right.

Which of the following are correct results of a Boolean expression?

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.


2 Answers

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 to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

like image 58
Rohit Jain Avatar answered Nov 15 '22 11:11

Rohit Jain


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.

like image 34
Martijn Pieters Avatar answered Nov 15 '22 13:11

Martijn Pieters