4 ms·
It's actually simpler then that hi = len(a) if hi is None else hi
by asperous 4y ago
It's actually simpler then that
hi = len(a) if hi is None else hi
- pansa2 4y agoYour `if-else` is more complex than the original that just uses `if`.
- eru 4y agoThe line itself is longer, but the idiom in the original uses mutation, which requires reading more adjacent lines to understand and make sure you haven't missed anything. (In the concrete example, both versions use mutation.)
- pansa2 4y agoI'm not sure I understand. Could you give a concrete example where only one version uses mutation?
- dragonwriter 4y agoAny example that does what the proposed ??= operator does, which is inherently conditional mutation, will use mutation. You can get examples equivalent to the proposed None-coalescing ?? operator does that don't use mutation, though.
- deleted 4y ago[deleted]
- thamer 4y agoThere's a major difference though: `value if predicate else alternative` is itself a value, something you can assign to a variable or pass to a function as a parameter.