This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Should this construct throw an exception?
Type: behavior Stage: resolved
Components: Interpreter Core Versions:
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, tladd
Priority: normal Keywords:

Created on 2021-02-04 21:52 by tladd, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg386496 - (view) Author: Tony Ladd (tladd) Date: 2021-02-04 21:52
The expression "1 and 2" evaluates to 2. Actually for most combinations of data type it returns the second object. Of course its a senseless construction (a beginning student made it) but why no exception?
msg386498 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-02-04 22:07
This is the expected behavior.


In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false.


The Boolean operators and and or are so-called short-circuit operators: their arguments are evaluated from left to right, and evaluation stops as soon as the outcome is determined. For example, if A and C are true but B is false, A and B and C does not evaluate the expression C. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument.
msg386500 - (view) Author: Tony Ladd (tladd) Date: 2021-02-04 22:43

Thanks for the explanation. Sorry to post a fake report. Python is relentlessly logical but sometimes confusing.
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87296
2021-02-04 23:42:03eric.smithsetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-02-04 22:43:18tladdsetmessages: + msg386500
2021-02-04 22:07:28Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg386498
2021-02-04 21:52:27tladdcreate