Skip to content

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

Conversation

spazm
Copy link

@spazm spazm commented Oct 7, 2020

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 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

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
```
@the-knights-who-say-ni
Copy link

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 username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@spazm

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!

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Dec 17, 2020
Copy link
Contributor

@MaxwellDupre MaxwellDupre left a 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.

@hugovk
Copy link
Member

hugovk commented Feb 19, 2022

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. 👍

@hugovk
Copy link
Member

hugovk commented Feb 19, 2022

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 with not. not has a higher precedence than and.

Sure, however we don't have not here, so parentheses are arguably redundant here, and for example, some IDEs will recommend removing them.

Thanks for the PR, but I'm not sure the addition necessarily helps with clarity in this case.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Jul 30, 2022
@hauntsaninja
Copy link
Contributor

Thanks, but +1 to hugovk here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants