T4r4nis
T4r4nis

Reputation: 31

Python makes wrong comparison of two booleans

I have this line of code:

print(isIndent(idxA), idxA[0]<idxB[0], isIndent(idxA) == idxA[0]< idxB[0])

In the code, I just test if the first statement has the same value as the second.

isIndent is a predicate and idxA and idxB are both tuples, but I run it I have this: True True False

So the two members are both True, but the equality is False

So as you might expect, I just want a way to compare reliably the two expressions in a single row (without creating any temp value storage....)

Upvotes: 2

Views: 68

Answers (1)

K4jt3c
K4jt3c

Reputation: 151

If you take a look at this part:

isIndent(idxA) == idxA[0]<idxB[0]

It says: if (isIndent(idxA) == idxA[0]) < idxB[0]

it uses comparison chaining (thanks @Barmar) Which means it becomes:

if (isIndent(idxA) < idxB[0]) and (idxA[0] < idxB[0])

Because of the order it was written in!

In order to change that behaviour, use parenthesis

print(isIndent(idxA), idxA[0]<idxB[0], isIndent(idxA) == (idxA[0]<idxB[0]))

Upvotes: 1

Related Questions