[issue36248] document about `or`, `and` operator.

New submission from Windson Yang <wiwindson@outlook.com>: I think we should document the behavior as below, (maybe at https://docs.python.org/3/reference/expressions.html#operator-precedence)
1 or 0 and 3 1 0 or 1 and 3 3
Please correct me if we already document it. ---------- assignee: docs@python components: Documentation messages: 337555 nosy: Windson Yang, docs@python priority: normal severity: normal status: open title: document about `or`, `and` operator. type: enhancement versions: Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36248> _______________________________________

Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment: It is already documented. Just follow the link from "or" or "and". https://docs.python.org/3/reference/expressions.html ---------- nosy: +serhiy.storchaka _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36248> _______________________________________

Steven D'Aprano <steve+python@pearwood.info> added the comment: Document *what* about the behaviour shown? I'm sure you don't mean to say that we should document the fact the *literally* `1 or 0 and 3` returns 1, but I don't know what you think we should document beyond what is already stated in the existing docs. It might be obvious to you, but it isn't obvious to me. ---------- nosy: +steven.daprano _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36248> _______________________________________

Windson Yang <wiwindson@outlook.com> added the comment: Thank you Serhiy, we did document here:
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
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.
Sorry, Steven. I should make it clear. I think the output of the example(1, 3) depends on the input order of number(1 or 0, 0 or 1) is not an expected behavior to me. Maybe we can add an example/note in the document. "Sometimes this will cause unexpected behavior when you put `or` and `and` together..." ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36248> _______________________________________

SilentGhost <ghost.adh@runbox.com> added the comment: Windson, it still is not clear what exactly you find unexpected in view of supplied links: 1 or 0 and 3 or has higher precedence than and, therefore it's evaluated first. It's first argument (1) is truthy, therefore it's returned. End of comparison. 0 or 1 and 3 starts the same, but now the second argument (1 and 3) needs evaluating and its return value (3) will be the end result. End of comparison. I certainly think the suggested wording is a no go. ---------- nosy: +SilentGhost _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36248> _______________________________________

Windson Yang <wiwindson@outlook.com> added the comment: SilentGhost, I think you give a great example to explain this behavior. If the behavior is obvious to you, we can close this issue. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36248> _______________________________________

Change by SilentGhost <ghost.adh@runbox.com>: ---------- resolution: -> not a bug stage: -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue36248> _______________________________________
participants (4)
-
Serhiy Storchaka
-
SilentGhost
-
Steven D'Aprano
-
Windson Yang