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

(resent because the CC address was incorrect)

On Sun, Nov 17, 2019 at 6:01 AM Kyle Stanley <aeros167@gmail.com> wrote:
> 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.

On Sun, Nov 17, 2019 at 3:45 AM Neil Girdhar <mistersheik@gmail.com> wrote:
Some people find this legal syntax confusing:

A in B < C

(It means 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?

If not, I suggest dividing the comparison operators into two groups:

in, not in, is, is not

and

<, >, <=, >=, ==, !=

and then disallowing chaining of operators from both groups.

For example, still allowed:

A <= B <= C

A in B in C

disallowed:

A < B in C

The advantage of such a change is that there is fewer "gotcha" code.  I admit that this code is rarely written, but it can come up.  For example, you might write your own comparison operator and then want to see if it's working:

A < B is None

returns false if B is not None even if A < B returns None.

Also, in my opinion, the existence is a deterrent to Python and takes away from Python's beauty as a language.

The downside of such a change is that the language is more complicated.

As far as implementation goes, this can be done after parsing when the AST is checked and errors are raised (from what I remember).

Best,

Neil
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/S7NBNTIPUON2N6WGNBCJXBRVAJNFPNPS/
Code of Conduct: http://python.org/psf/codeofconduct/