Why in Python it is evaluated this way:
>>> False is False is False
True
but when tried with parenthesis is behaving as expected:
>>> (False is False) is False
False
Why in Python it is evaluated this way:
>>> False is False is False
True
but when tried with parenthesis is behaving as expected:
>>> (False is False) is False
False
In this case, each of
False
pair is evaluated. The first two False is evaluated, if it'sTrue
, then the second and the thirdFalse
is evaluated and return the result.In this case,
False is False is False
is equivalent toand
of results of 2 commandsFalse is False
Your expression
is treated as
So you get
and that evaluates to
True
.You can use this kind of chaining with other operators too.
Chaining operators like
a is b is c
is equivalent toa is b and b is c
.So the first example is
False is False and False is False
, which evaluates toTrue and True
which evaluates toTrue
Having parenthesis leads to the result of one evaluation being compared with the next variable (as you say you expect), so
(a is b) is c
compares the result ofa is b
withc
.Quoting Python official documentation,
So,
False is False is False
is evaluated asThe second
False is False
expression uses the secondFalse
in the original expression, which effectively translates toThat is why the first expression evalutes to be
True
.But in the second expression, the evaluation sequence is as follows.
Which is actually
That is why the result is
False
.I think that
False is False is False
means(False is False) and (False is False)
, but(False is False) is False
means :So, you get the result.