0

I made a programming error in an algorithm which I didn't notice for a period of time. Doesn't matter the results were good and we wanted to implement this production in the productive system (model in python, productive system in c++).

So the first if statement in this algorithm should end the calculation if c == 0 or b == False Unfortunately the statement was programmed like this:

if c == 0 is not b:
    return

Instead of:

if c == 0 or not b:
    return

My question now is, why is the first if statement no syntax error? I don't see any logical statement this should evaluate? Can someone explain as what the python interpreter interpretes this statement?

To understand the behaviour I calculated the truth table:

c |   b   | result
-------------------
0 | False | True
0 | True  | True
1 | False | False
1 | True  | False

So it seems that python tests for c == 0 and ignores the is not b, what ever this is meant to be. In my eyes it checks if the temporarily True or False statement is not the same object as b, what it never is, therefore we get the above resulting truth table. Is this thought correct?

RoQuOTriX
  • 2,871
  • 14
  • 25
  • 3
    `c == 0 is not b` is the same as `(c == 0) and (0 is not b)`. You can find it in the documentation: [Comparisons](https://docs.python.org/3/reference/expressions.html#comparisons) – Matthias Dec 16 '19 at 12:31
  • 1
    Ah I got it, 0 is NEVER b and therefore always true – RoQuOTriX Dec 16 '19 at 12:41

0 Answers0