3 ms·
The difference between Ruby and Python here is that Ruby has a perfectly functional ternary operator that works as one would expect. It also allows some other
by Lazare 10y ago
The difference between Ruby and Python here is that Ruby has a perfectly functional ternary operator that works as one would expect. It also allows some other crazy syntax which can be appropriate in some cases, but it doesn't force you to use it in all cases.
Python does force you to use their awkward conditional expressions; there is no normal ternary operator.
There's a big difference between a language which lets you write hideous code, and a language which forces you to write hideous code.
- TheDong 10y agoPython does not force you to. In any case where you can use PEP 308, you can use a boring traditional "if else".
- jolux 10y agoBut you can't use a normal ternary operator...
- Lazare 10y agoIs that intended as a defense of Python's ability to enable clear, concise, expressive code? :) More seriously: Yes, you are obviously correct. But while you can use if/else, you can't use a ternary, and you can in Ruby. That's why people complain about the lack in Python but don't wrote the same article about Ruby.
- luord 10y ago"Clear, concise, expressive code" isn't something that applies to ternary operators. They were always meant as shortcuts and, as such, will never be as clear as if else statements, practically by design. Though I don't really dislike Python's approach so what do I know.
- jolux 10y agoYou can definitely use too many ternary operators, but I think when used sparingly they can be clear and tasteful. As long as you don't start nesting them or repeating them one after the other you should be fine.