Your code is almost optimal when it comes to code repetition and evaluation. The only thing I can think of to avoid repetition would be:
# be optimistic! either_true = True if x: do_something1 elif y: do_something2 else: either_true = False if either_true: do_something3
This removes one assignment, although the total number of rows does not change.
The advantage is that this works with conditions n without adding any other assignments, while the current solution requires either_true = True for each condition.
In my opinion, they have the same degree of readability, but the code above will be better with a lot of conditions.
There is also no “pythonic” method other than the readable one, which avoids code repetition and is optimal in terms of efficiency, and I don’t know of any “better programming” to achieve the same result.
Bakuriu Feb 13 2018-12-14T00: 00Z
source share