Reputation: 75
I tried to do a comparison like the code below with Python, but instead was confused by the output produced.
Can anyone explain why the output is like this?
>>> True, True == True, True
(True, True, True)
>>> True, True == (True, True)
(True, False)
Upvotes: 4
Views: 783
Reputation: 780663
Because of operator precedence. ==
has higher precedence than ,
, so the first expression is treated as if you'd written
True, (True == True), True
Your second expression is treated as
True, (True == (True, True))
If you want to compare the two sides of ==
element-wise, you need to parenthesize both sides:
(True, True) == (True, True)
This will return True
.
Note that comma is not strictly an operator, so but for the purposes of understanding this behavior it's close enough.
Upvotes: 4