-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Reconsider comparison chaining for containment tests #76236
Comments
(I thought there was an open low priority issue for this, but I can't find it, so filing a new one) Currently, "in" and "not in" are classified as comparison operations in the language grammar, even though they're not actually documented as such (see https://bugs.python.org/issue28617 in relation to the latter point). Issue reports like https://bugs.python.org/issue30965, user questions like https://stackoverflow.com/questions/45180899/unexpected-result-from-in-operator-python/45180967, and behaviour puzzles like https://github.com/cosmologicon/pywat#operator-precedence suggest that the existing behaviour isn't particular intuitive to users. At the language design level (as far as I am aware), the benefit of treating "in" and "not in" as comparison operators is to ensure they share a precedence level with the other comparisons. While this is mostly a pretty harmless quirk, I think it's weird enough and useless enough for us to at least consider refining the Grammar such that "in" and "not in" live at the same level as other comparison operators, but *don't* participate in comparison chaining (i.e. "a == b in c" and "a in c == b" would both be syntax errors that required parentheses to disambiguate the desired associativity). |
https://bugs.python.org/issue14247 is another older issue related to this point (again related to a user finding the current behaviour genuinely unintuitive) |
|
Aye, there are definitely cases where the answer isn't nonsense. Even for sets though, "a in b < c" isn't an intuitive spelling of "a in b and b < c" the way that "a < b < c" is an intuitive spelling of an ordering relation. Hence filing this as a low priority issue - it's a weird quirk, and potentially worth changing to avoid a particular "Wat?" moment when folks first see it, but it isn't a bug magnet the way some other historical constructs have been. |
Just a note on why I find "a in b < c" unintuitive, even for sets: my brain initially wanted to read it as equivalent to "a in b & c". |
PR 4501 makes the parser emitting a SyntaxWarning for chained But this should be first discussed on Python-Dev. |
If we do decide to do this, I'm inclined to eventually make the change at the Grammar level rather than the AST level. Current:
Future:
However, we'd still need an intermediate step like your PR in order to emit SyntaxWarning while still retaining the current semantics. |
I'm reading the list of most rated in a week questions on StackOverflow (https://python-weekly.blogspot.com/), and it seems to me that the question about chained "in" and "not in" is raised every few months. This may be the one of the most confusing quirks. |
On other hand, beginners are confused even by chained "==". There are regular reports about this on the tracker and questions on Python-related resources. I have found the chained "in" is useful in the following case
for testing if the string is quoted with a single or double quotes. |
Has this been discussed on python-dev? If so, what was the result? |
This had not been discussed. |
I agree that it would be less confusing if Then again if we're going to forbid (or even discourage) unusual combinations we might also want to frown at Finally as long as we're refining the terminology, maybe we could strive to distinguish "comparison" ( |
in
andnot in
. #4501Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: