Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion related to Python's `in` operator [duplicate]

Tags:

python

I found strange behavior with Python's in operator

d = {}
'k' in d == False # False! 

I thought it's because of precedence:

('k' in d) == False # True, it's okay
'k' in (d == False) # Error, it's also okay

But, what precedence evaluates the following expression then?

d = {}
'k' in d == False

If it's because of wrong precedence why it doesn't fire an error like if:

'k' in (d == False)

In other words, what happens under the hood of Python with this expression?

'k' in d == False
like image 311
Nikita Avatar asked Jul 31 '20 12:07

Nikita


1 Answers

in is considered a comparison operator, and so it is subject to comparison chaining.

'k' in d == False

is equivalent to

'k' in d and d == False

because both in and == are comparison operators.

You virtually never need direct comparison to Boolean literals, though. The "correct" expression here is 'k' not in d.


For reference, this is described in the Python documentation, under 6.10. Comparisons:

comparison    ::=  or_expr (comp_operator or_expr)*
comp_operator ::=  "<" | ">" | "==" | ">=" | "<=" | "!="
                   | "is" ["not"] | ["not"] "in"

and

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 107
chepner Avatar answered Sep 18 '22 13:09

chepner