| Article: |
Test-Driven Development in Python | |
| Subject: | Why the code failed | |
| Date: | 2005-03-30 19:33:11 | |
| From: | Gancho | |
Your code,
failed the testMatchesFalse test because == binds more tightly than "or."
|
||
Showing messages 1 through 1 of 1.
| Article: |
Test-Driven Development in Python | |
| Subject: | Why the code failed | |
| Date: | 2005-03-30 19:33:11 | |
| From: | Gancho | |
Your code,
failed the testMatchesFalse test because == binds more tightly than "or."
|
||
Showing messages 1 through 1 of 1.
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.