9 ms·
I'm not much of a Python guy, but that chained comparison operator is sweet! Sure, it's just syntax sugar, but it saves a lot of keystrokes, especially if the
by desdiv 12y ago
I'm not much of a Python guy, but that chained comparison operator is sweet!
Sure, it's just syntax sugar, but it saves a lot of keystrokes, especially if the variable name is long.
Is Python the only language with this feature?
- euphemize 12y agoI'm not sure if other languages have it, but I have to say that pycharm is excellent at suggesting chained comparisons to you. I didn't know this existed before I switched IDEs. if 2 > 3 and 2 < 7 becomes if 3 < 2 < 7
- kazinator 12y agoIf this Pycharm really had brains it would tell you that (2>3 and 2<7) reduces to (false and true), to false, at compile time, so the code wrapped in the if is unreachable.
- Alphasite_ 12y agoI'm not sure about pycharm, but i know for certain IntelliJ warns you about constant conditions like that. That said, he could have picked that example just to illustrate a point, rather than something it literally suggests.
- dragonwriter 12y ago> Is Python the only language with this feature? No. See, e.g., http://stackoverflow.com/questions/4090845/language-support-for-chained-comparison-operators-x-y-z http://stackoverflow.com/questions/4090845/language-support-...
- pwr22 12y agoPerl6 has it too > perl6 > 3 < 4 < 5 True
- dllthomas 12y ago3 < 4 < 5 evaluates to true (well, 1) in C as well. What does 5 > 4 > 3 give?
- kazinator 12y agoCommon Lisp (and other dialects): (< a b c d) ;; T if a < b < c < d (<= a b c d) ;; T if a < b < c < d Also: (lcm a b c d ...) ;; lowest common multiple (+) -> 0 (+ a) -> a (+ a b) -> a + b (+ a b c) -> (a + b) + c (*) -> 1 (* a) -> a (* a b) -> a * b (* a b c) -> (a * b) * c Is it just syntactic sugar? (< a b c) evaluates a, b and c only once, which matters if they are expensive external function calls or have side effects. (and (< (foo) (bar)) (< (bar) (baz))) isn't the same as (< (foo) (bar) (baz)) By the way, this could be turned into a short-circuiting operator: more semantic variation. Suppose < is allowed to control evaluation. Then an expression like (< a b c d) could avoid evaluating the c and d terms, if the a < b comparison fails.
- hasenj 12y agoBut can any lisp dialect do: a < b >= c ?
- baddox 12y agoGiven that lisps tend to use prefix notation, the example doesn't even translate meaningfully.
- dllthomas 12y agoI assume the above was intended to be a Python expression. The answer, to the best of my knowledge, is "not really". There is no built in or reasonably standard function that lets you check simultaneously that a is less than b which in turn is greater than or equal to c. Not that we couldn't define one ad-hoc, although making it reusable in a way that's reasonably idiomatic might be a challenge... Obviously, you can express it slightly more verbosely: (and (< a b) (>= b c))
- davidrusu 12y ago(< a (>= b c)) same number of operators but a few extra parens
- deleted 12y ago
- jfoster 12y agoI think syntactic sugar is vastly undervalued by programmers. Anything that lets me more naturally read & write code should lead to fewer bugs and more productive software development, as well as making programming more enjoyable.
- teddyh 12y agoSQL has “x BETWEEN y AND z” as a special case which does “y >= x >= z”, which in practice is quite often what you actually want to use this feature for.
- Zecc 12y agoIt's not simply syntax sugar either. def sideEffects(): print "Called." return 5 if 0 < sideEffects() < 10: # sideEffects is only called once