How to confuse a Python programmer
- 1 min readWhile browsing the internet, I came across the following line of Python code:
True, True, True == (True, True, True)
Counter-intuitively, if you execute the statement, you will end up with:
(True, True, False)
How is this possible I asked myself? (Yes I was a confused Python programmer myself at that point). Well, the answer is relatively simple: ==
does not do the unpacking for us. So, as a human you think that Python does something along the lines of (True, True, True) == (True, True, True)
or True, True, True == True, True, True
. However, Python actually interprets the line as follows:
True, True, (True == (True, True, True))
This means that it first evaluates whether (True == (True, True, True))
, which is False
. Then it returns the rest of the statement as a tuple:1 (True, True, False)
.
Another case that can be explained in the same way is False is False is False
. Can you guess what the result is when you execute that statement? Hint: Python evaluates the statement as (False is False) & (False is False)
, which results in True
. Now that makes sense, right?