Test-Driven Development in Python
Subject:   Why the code failed
Date:   2005-05-14 17:14:16
From:   cpgray
Response to: Why the code failed

But "c and c == (2 or True)" is no longer of the form "a and b or c" as a simulation of "a ? b : c".

The reason the code fails is that the simulation isn't exact. If a is True, "a ? b : c" returns b.
If a is True, "a and b or c" evaluates b. If b evaluates to True, evaluation stops and b is returned. If b evaluates to False (or None or 0 or [], etc.), evaluation continues and c is evaluated and returned. In other words, you'll never return b if b evaluates to False.

For example "1 == 1 and 2 or 3" returns 2 as expected, but "1 == 1 and 0 or 3" returns 3.