[docs] Another strange expression, 2

Al Sweigart asweigart at gmail.com
Tue Jul 10 11:39:35 EDT 2018


This is an unfortunate side effect of chaining the == and in operators.
Look at this equivalent expression:

>>> spam = False
>>>  True == spam in (True, False)
False

The expression `True == spam in (True, False)` is equivalent to `(True ==
spam) and (spam in (True, False))`, which is why it evaluates to False.

This is similar to how `10 < spam < 20` is equivalent to `(10 < spam) and
(spam < 20)`, and not `(10 < spam) < 20` or `10 < (spam < 20)`

The lesson is to not mix different kinds of operators when chaining them.

-Al

On Tue, Jul 10, 2018 at 2:24 AM, Vrancken, J.L.M. (Jos) <
j.l.m.vrancken at hr.nl> wrote:

> True == False in (True, False)
>
>
>
> This expression gives False,
>
> whereas in any interpretation, it should be True:
>
>
>
> (True == False)  in (True, False)        returns True
>
>
>
> True == (False in (True, False))        returns True
>
>
>
> All this in Python 3.7.
>
>
>
> Is something wrong or am I'm wrong in thinking something's wrong here?
>
>
>
> Jos Vrancken
>
> _______________________________________________
> docs mailing list
> docs at python.org
> https://mail.python.org/mailman/listinfo/docs
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20180710/1a052ff8/attachment-0001.html>


More information about the docs mailing list