[issue38060] precedence (relational, logical operator)not working with single value

Tim Peters report at bugs.python.org
Tue Sep 10 12:49:39 EDT 2019


Tim Peters <tim at python.org> added the comment:

Ah, so you were expecting an error!  That helps.

But that's not how the language works, or how it's documented to work, as has been explained in quite some detail already.

In general, precedence _constrains_ evaluation order, but does not _define_ it.  In Python or any other language.  In

     9 or 7 > "str"

the precedence rules say the expression groups as

     9 or (7 > "str")

rather than as, say,

     (9 or 7) > "str"

but says nothing more than just that.  It's a very intentional - and documented - feature of "and" and "or" that they do NOT evaluate their right-hand operands at all if the result of evaluating the left-hand operand first is enough to settle the issue.  The precedence rules only define what the "left-hand" and "right-hand" operand expressions _are_.

I don't think the docs could be materially clearer about this.  For example, from section "6.11. Boolean operations":

"""
The expression `x or y` first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
"""

Precedence rules alone are too feeble to capture that.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38060>
_______________________________________


More information about the Python-bugs-list mailing list