classification
Title: Should this construct throw an exception?
Type: behavior Stage: resolved
Components: Interpreter Core Versions:
process
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 2021-02-04 23:42 by eric.smith. 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 triager) Date: 2021-02-04 22:07
This is the expected behavior.

From https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming

"""
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.
"""


From https://docs.python.org/3/tutorial/datastructures.html?highlight=short%20circuit#more-on-conditions

"""
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
Dennis

Thanks for the explanation. Sorry to post a fake report. Python is relentlessly logical but sometimes confusing.
History
Date User Action Args
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