Tim Peters added the comment:
I don't believe that would improve the docs, but suit yourself. This is hardly
a FAQ, but instead a peculiar case where, for whatever reason, someone is
saying "I'm puzzled by what `or` does, but didn't read the docs for `or`".
Most people confused by `or`
Steven D'Aprano added the comment:
I think that the problem is that the precedence table may be technically
correct, but it doesn't describe the actual behaviour of expressions
including the boolean operators ``or`` and ``and`` for exactly the
reason Tim gives:
> Precedence rules alone are
Tim Peters 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
Sangeeta M Chauhan added the comment:
Sir, I was expecting that the precedence should be given to relational
operator ( 7>"str") and according to that instead of printing 9 it
should give error.
On Mon, Sep 9, 2019 at 8:38 PM Tim Peters wrote:
>
> Tim Peters added the comment:
>
>
Tim Peters added the comment:
BTW, the docs also spell out that "and" and "or" ALWAYS evaluate their left
operand before their right operand, and don't evaluate the right operand at all
if the result of evaluating the left operand is true (for "or") or false (for
"and"). So, e.g.,
Tim Peters added the comment:
@sangeetamchauhan, the reply you got in the image you attached was in error -
kind of. Section "6.16. Operator precedence" defines Python's operator
precedence:
https://docs.python.org/3/reference/expressions.html#index-92
"""
The following table summarizes
Steven D'Aprano added the comment:
Tim is correct, the behaviour is right, however the docs could be clearer.
I think what you are missing is that ``or`` and ``and`` are short-circuiting
operators. So in the expression
9 or (anything)
the "anything" expression never gets evaluated
Tim Peters added the comment:
I'm sorry you're not satisfied with the answer, but I'm a bona fide expert on
this and you're not going to get anyone to agree with your confusion here ;-)
But the bug tracker is not the right place for tutorials. Please take this up
on, e.g., the Python
Sangeeta M Chauhan added the comment:
i am not satisfied ..with your answer . as in the following expression 9
or 7 > "str" precedence must be given to relational operator . why is is
executing logical operator first??
if we write 4>9 or 7> "str" it works correct but if we
Tim Peters added the comment:
It's working fine. What do you expect? For example,
9 or 7 > "str"
groups as
9 or (7 > "str")
9 is evaluated for "truthiness" first, and since it's not 0 it's considered to
be true. That's enough to determine the result of "or", so (7 > "str")
New submission from Sangeeta M Chauhan :
precendence betweeen relational and logical operators not working properly if
expression contains single values instead of sub expressions. . Please see
attached file
--
components: Interpreter Core
files: pythonBug.py
messages: 351344
nosy:
11 matches
Mail list logo