6 ms·
I like chained comparisons for the `10 < x <= 100`, since it makes intuitive sense and removes duplication. But I can't think of any case with `==` type operat
by BurningFrog 7y ago
I like chained comparisons for the `10 < x <= 100`, since it makes intuitive sense and removes duplication.
But I can't think of any case with `==` type operators, or really any other operators where it also makes sense.
So was that maybe an overgeneralized feature that should have been limited to the math operators?
- klodolph 7y agoI can think of a few cases where I want: if x == y == z:
- nogridbag 7y agoI always found prefix notation in Clojure to be really elegant for a lot these cases that are odd in other languages: (= x y z)
- ramshorns 7y agoThis is a bit like something you can do in C, almost by accident as a bonus language feature. x = y = z = 4; But I think this is assignment expressions which were controversial in Python or something.
- mark-r 7y agoNot anymore, check out the walrus operator.
- sk0g 7y agoYeah in Python you can do, say, a = b = [] But then the two lists are the same, and appending to one appends to the other. Not particularly useful.
- Ragib_Zaman 7y agoIt's useful when the object being assigned to those variables are literals instead of reference types, which in my experience is most of the time when you want to do the simultaneous assignment. When you do want to do reference types, you can do a,b = [], [].
- epistasis 7y agoI could see this for something like foo == bar == baz But chaining the == with other operators feels weird, especially "is". Similarly foo >= bar <= baz feels very off to me, and I'm not sure it should be chainable. If the intuition is to emulate human notation in math, there are many chained expressions that we would not allow to happen.
- marcosdumay 7y ago> But chaining the == with other operators feels weird Chaining anything with `is` feels weird to me, but: foo >= bar == baz >= fooo fells perfectly ok.
- throwaway_pdp09 7y ago(please ignore, I'm an idiot) (deleted)
- baq 7y agoNot OP but I’d expect no precedence as I expect this to parse into a series of number comparisons and-chained.
- throwaway_pdp09 7y agoDamn, you're right, was being stupid. Duh.
- nerdponx 7y agoI agree, "is" should probably be excluded from comparison chaining.
- deleted 7y ago[deleted]
- thomasahle 7y agoI guess if you say "x == y == z" you may also say "x is y is z". But certainly the mixing of the two chaining types is confusing.
- pansa2 7y agoMaybe it should be even more limited than that. For example, chains of `==`, mixed `<` and `<=`, or mixed `>` and `>=` only. That would disallow unintuitive things like: if a <= b > c: But then, what to do in those disallowed cases? Is the parser powerful enough to make them SyntaxErrors? If not, I’d much rather have the above compiled into the unintuitive `a <= b and b > c` than the completely wrong `(a <= b) > c`.
- ben509 7y agoA context-free grammar can do it; you'd have three productions "ascending comparison chain," "descending comparison chain" and "equality chain" to cover the valid possibilities. But, yeah, if there are contradictory comparisons, that should be deprecated and raise an error immediately because it indicates a logic error. If you're doing something awful like using custom operators for side-effects, just write out (a <= b) and (b > c). Most likely, though, no one is using custom operators for side-effects in chaining because the implicit `and` coerces arguments to booleans. So, with numpy, you can compare two ndarrays with a < b and it returns a new array of booleans, but you can't chain compare three ndarrays because `and` coerces the result to a single boolean.
- Non24Throw 7y agoI think in language design there’s an expectation that if an expression or statement doesn’t make any sense, then people won’t write it that way. I think that’s a pretty reasonable expectation, too. In JavaScript, {} + [] evaluates to integer 0. That doesn’t make any sense, but it makes more sense after reading the ES spec for the addition operator. There are many expressions you can write in dynamically typed languages that don’t make any sense, probably most of them actually, but they have to be considered valid because it’s a dynamically typed language. So they’re valid, they will evaluate to something. The language designers aren’t so concerned with identifying every possible combination that makes no human-intuitive sense. The important part is that when it seems like types should be inferred and coerced in a particular way, then that’s how it should work. It should match human intuition. I don’t have any intuition or opinion about how True == False is False should be evaluated, this kind of thing is going to receive superfluous parentheses from me every time for the benefit of the reader, and if someone else wrote it this way I’m always going to look it up or test it in a REPL... 10 < x <= 100 though, if that’s considered a valid expression and it doesn’t evaluate to true for numbers in the range (10,100], I’m going to stop using that language...
- pansa2 7y ago> There are many expressions you can write in dynamically typed languages that don’t make any sense, probably most of them actually, but they have to be considered valid because it’s a dynamically typed language. Not really - a language could throw an exception in these cases, as Python does for things like “a”+1. Not every dynamically-typed language is JavaScript.
- mantap 7y agoThat is the difference between a dynamically typed language (like Python) and an untyped language (like JS). The dynamically typed language checks types at runtime, whereas the untyped language doesn't.
- shrimpx 7y agoIt’s really not dynamic vs untyped, but the extent to which the language design chooses to coerce a value into a given type instead of raising a runtime type error. Python is guilty of this too with its truthy values, e.g. `if []` — an empty list of type `list` is suddenly apparently inhabiting the bool type and is equivalent to False.