poniedziałek, 27 sierpnia 2018

False is False is False ?



Hello!
Today I have a little puzzle for you. Probably you know how to evaluate this line: 


False is False

Of course, False indeed is False, so the result is True. But what if someone will ask you a bit more tricky question. What is the result of:


False is False is False

Probably you will take the same approach as I did when I saw this for the first time: if False is False evaluates to True, then we can simplify the problem to:


True is False

Which, obviously, is False.
Then it means, that the solution to the post question should be: False.
But is it?
Try to type it into your console, and then come back here!


~~~~~

Hello again!
Now you are probably scratching your head wondering what happened. Why False is False is False evaluates to True?
Well, it's exactly the same behavior as in the:


1 < 2 < 3

Which also evaluates to True
The reason for this is that due to Python's logic the chained operators like:
a is b is c are equivalent to a is b and b is c.
So False is False is False is treated like:
False is False and False is False, which is True.

For more information check the official Python's comparisons documentation here.

Brak komentarzy:

Prześlij komentarz