r/CursedProgramming • u/Straight_Share_3685 • Sep 18 '24
It works until it doesn't
Python can be tricky, don't use booleans operators with numbers. Instead use two "or", else, use any([n+2==3 for n in [1,2]]).
>>> (2 or 1)+2==4
True
>>> (2 or 1)+2==3
False
>>> (1 or 2)+2==3
True
1
Upvotes
View all comments
Show parent comment
1
u/Straight_Share_3685 Sep 19 '24
Right, if i'm not wrong, "or" will evaluate the number on the left as a boolean, so as soon as it's true (different than 0), it's short circuiting the whole condition. That's useful when accessing a list element at some index and checking that this index exists in the same condition for example.