> Some people find this legal syntax confusing:
> A in B < C
I agree, that syntax definitely seems counter-intuitive. If it came up in a code review, I would request a revision to "A in B and B < C".
> I feel like most code reviews would not allow this kind of code to
be checked in, at least without a comment explaining what the code
does. What are the motivating reasons for allowing this syntax? Is
there any idiomatic code that uses it?
I can't personally imagine any practical use case for the syntax that couldn't be expressed differently in a way that's far more clear to readers. But, I don't think this is so much of a case of us choosing to allow the syntax, this seems to be more of a case where we don't explicitly prevent the syntax (through raising a SyntaxError).
From my experience, SyntaxError is raised primarily from clear mistakes that are easy to make (particularly for those who are new to the language or accidental typos) rather than simply syntax that is obscure, impractical, and/or hard to understand. It's realistically impossible to isolate every case of highly counter-intuitive usage of Python syntax, not to mention the resources it takes to implement a SyntaxError for each of them.
To me, it seems that the usage of "A in B <comparison_operator> C" is something that's not easy to do by accident, I can't imagine what someone would be intending to practically accomplish and mistakenly end up with that syntax, not knowing it could be expressed differently. A few important questions to consider:
1) Is this syntax misused enough to justify implementing a SyntaxError?
2) Would raising a SyntaxError help to point those who misuse it in the right direction?
3) Is it worth the resources it would take to implement the SyntaxError?
4) Does implementing the SyntaxError provide enough positive benefit to justify breaking backwards compatibility? Even if it's very rarely used and could be expressed differently, this still applies.
> Also, in my opinion, the existence is a deterrent to Python and takes away from Python's beauty as a language.
I would have to disagree, every language has its obscure corner cases and confusing ways to write expressions. I don't think the number of ways that users can misuse the language in an obscure manner to write confusing code should at all detract from it's beauty. If it were easy to misuse the language and accidentally write confusing code that would be one thing, but I don't think "A in B < C" is an example of that scenario.
As the others mentioned, I think the best solution to dealing with this type of syntax would be with a linter rather than directly preventing its usage.