classification
Title: document about `or`, `and` operator.
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: SilentGhost, Windson Yang, docs@python, serhiy.storchaka, steven.daprano
Priority: normal Keywords:

Created on 2019-03-09 06:24 by Windson Yang, last changed 2019-03-09 11:33 by SilentGhost. This issue is now closed.

Messages (6)
msg337555 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-03-09 06:24
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.
msg337556 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-03-09 06:30
It is already documented. Just follow the link from "or" or "and".

https://docs.python.org/3/reference/expressions.html
msg337558 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-03-09 06:50
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.
msg337564 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-03-09 09:06
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..."
msg337565 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-03-09 09:27
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.
msg337568 - (view) Author: Windson Yang (Windson Yang) * Date: 2019-03-09 09:55
SilentGhost, I think you give a great example to explain this behavior. If the behavior is obvious to you, we can close this issue.
History
Date User Action Args
2019-03-09 11:33:10SilentGhostsetstatus: open -> closed
resolution: not a bug
stage: resolved
2019-03-09 09:55:27Windson Yangsetmessages: + msg337568
2019-03-09 09:27:45SilentGhostsetnosy: + SilentGhost
messages: + msg337565
2019-03-09 09:06:11Windson Yangsetmessages: + msg337564
2019-03-09 06:50:16steven.dapranosetnosy: + steven.daprano
messages: + msg337558
2019-03-09 06:30:11serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg337556
2019-03-09 06:24:24Windson Yangcreate