On 2/13/2014 4:59 PM, Ram Rachum wrote:
Hi everybody,
Please excuse the recent torrent of crazy ideas :)
I was reading code of the Shpaml project, trying to make a patch, while I saw code that looked inelegant to me. I considered how to improve it, but then saw I don't know how to.
The code paraphrased is this:
if expensive_computation_0(): x = expensive_computation_0() # Do something with x... elif expensive_computation_1(): x = expensive_computation_1() # Do something with x... elif expensive_computation_2(): x = expensive_computation_2() # Do something with x...
I do not really understand the fear of indents that would cause one to repeat calculations rather than write the actual logic. x = expensive_computation_0(): if x: # Do something with x... else: x = expensive_computation_1() if x: # Do something with x... else: x = expensive_computation_2() # Do something with x... If the code is already indented so much that 8 more spaces is a burden, then temporarily use 1 space indents. If do_something is the same, I would use 'or' as already posted. Python 'or' expressions are flow-control expression, not just logic expresssion. If there are more nested clauses, a separate function or "while 'fake loop': ... break" is fine. -- Terry Jan Reedy