-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Docs: improve equivalence expression for chained comparison #22589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs: improve equivalence expression for chained comparison #22589
Conversation
Add grouping parenthesis around the equivalence expression to make it clear that it is evaluated as a single expression. `x < y < z` parses as a single expression, so while it is equivalent to x < y and y < z in isolation, it is different when used in an expression with `not`. `not` has a higher precedence than `and`. We don't see this with `or` as it has a lower precedence than `and`. (See the next section on Boolean Expressions) + `not x < y < z` == `not (x < y < z)` + `not x < y and y < z` != `not (x < y and y < z)` ``` >>> import ast >>> ast.dump(ast.parse('not x<y<z')) == ast.dump(ast.parse('not(x < y <z)')) True >>> ast.dump(ast.parse('not x < y and y < z')) == ast.dump(ast.parse('(not x < y) and y < z')) True >>> ast.dump(ast.parse('not x < y and y < z')) == ast.dump(ast.parse('not (x < y and y < z)')) False ```
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA). Recognized GitHub usernameWe couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames: This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. You can check yourself to see if the CLA has been received. Thanks again for the contribution, we look forward to reviewing it! |
This PR is stale because it has been open for 30 days with no activity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its usual to have an issue opened in https://bugs.python.org/
I suggest just copying the text from you conversation.
For documentation changes, we can generally skip an issue and news file. I've added the appropriate labels. 👍 |
Sure, however we don't have Thanks for the PR, but I'm not sure the addition necessarily helps with clarity in this case. |
Thanks, but +1 to hugovk here |
Add grouping parenthesis around the informal equivalence expression to make it clear that it is evaluated as a single expression.
x < y < z
parses as a single expression, so while it is equivalent to x < y and y < z in isolation, it is different when used in an expression withnot
.not
has a higher precedence thanand
. We don't see this withor
as it has a lower precedence thanand
. (See the next section on Boolean Expressions)not x < y < z
==not (x < y < z)
not x < y and y < z
!=not (x < y and y < z)