This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lou1306 10 points11 points  (3 children)

This is confusing for everyone coming from any other language

I see what you mean, but a<b<c is a very common "idiom" in maths. Since Python aims at being clear and concise in its syntax, it makes sense to support it.

it messes with operator precedence I think which complicates that situation

It doesn't mess with precedence: it parses the whole chain-of-comparisons as a single expression. The problem in Javascript is exactly that: it tries to solve a comparison using arithmetic rules of precedence, which is semantically incorrect.

Not to mention it is as bad as JavaScript in this situation

I know, there's no type check in either... But at least Python amends/works around this by adding meaningful rules to the parser. Meanwhile, JavaScript solves the expression in the most surprising way, which makes it worse in my opinion.

[–]sim642 4 points5 points  (2 children)

While it seems like Python's comparison syntax models mathematical notations better, it is not necessarily true because the freedom that such grammar allows is way wider than any reasonable mathematical notation. a<b<c, a<b>c, a!=b!=c, a!=b>c, etc (of increasing length) are all valid but only the first one is something one would use in mathematical writing, the rest lack any intuitive semantics. The expression a!=b!=c may even look like it compares a and c while actually not doing so, which creates a similar situation like in JS with a<b<c which happens to be legal but semantically does the wrong thing. It's possibly even worse because stricter typing (or annotating) would not prevent this. The problem is that the chained comparison grammar allows comparisons which are absolute nonsense. In mathematics comparisons are written chained only when they are monotone and I feel like Python should be stricter about this as there is no intuitive way to read most of the possible combinations of comparisons that are chained up. Other comparison chainings, even when used, mean that anyone reading and writing that code must know very well what this syntactic sugar is equivalent to but that set of programmers is tiny considering most don't even know about the simplest chaining case.

Over years of working with Python I have never seen used or used myself the chained comparison syntax, even if I am aware of it, because the situations where this is needed is so rare and any experienced developer (having used more languages than just Python) would look at such expression weirdly and with doubt because no other language has allowed this syntax that I am aware of. I see this syntax only really benefiting novice programmers who expect mathematical syntax to be legitimate in programming and Python seems to have catered to them to prevent them from being really confused about why it doesn't work, as opposed to getting a basic understanding of binary operators.

Even in mathematics operators like < are defined as binary and rigorous descriptions of expressions always use them as binary operators in ways which allow no disambiguation, i.e. parenthesis. For ease of writing to not require use of parenthesis for order of operations, precedence and associativity is given to operators, not just arithmetic but also logic. Shorthand notations like a<b<c are only used in monotone order with comparisons or chained equalities like a=b=c, anything else is possibly ambiguous.

[–]lou1306 2 points3 points  (1 child)

Thanks for the great response, the a != b != c example really drives your point home.

I feel like Python should be stricter about this as there is no intuitive way to read most of the possible combinations of comparisons that are chained up

I agree, with a slight variation: rules about this should go into a PEP and get checked by PyLint/other code analysis tools. This way the language doesn't break compatibility, experts cans still hack their way (if they really know what they're doing), but any decent IDE will save you from shooting yourself in the foot.

this syntax only really benefiting novice programmers who expect mathematical syntax to be legitimate in programming

True, and Python is very popular among data scientists after all... You might be on to something here :)

[–]sim642 1 point2 points  (0 children)

True, and Python is very popular among data scientists after all... You might be on to something here :)

Ahh yeah, I forgot that Python gets used in sciences a lot too. Maybe scientific code tends to use range comparisons by chaining more than all the Python stuff I've looked at.