For review: PEP 308 - If-then-else expression
Carlos Ribeiro
cribeiro at mail.inet.com.br
Sat Feb 8 19:06:30 EST 2003
On Saturday 08 February 2003 02:51 pm, Andrew Koenig wrote:
> Andrew> OTOH, I still say this is easier to debug, because I can do
>
> Andrew> if i < 0:
> Andrew> print "Negative?", i
> Andrew> col = None
> Andrew> else:
> Andrew> print "i", i, "neighbors", cols[max(i-1, 0):i+1]
> Andrew> col = cols[i]
>
> Yes indeed. The flip side comes when you do something like this a bunch
> of times and your function no longer fits on the screen.
Thanks again for pointing out the type of situation where I expect conditional
expressions to be most useful. I find it amazing that many people don't
regard long blocks of code as a problem; as for myself, I get lost with
blocks that don't fit on the screen.
One can easily divide conditional statements in any given program in two
classes. The first one is the core logic of the program; other conditions are
'auxiliary', as they are 'helpers' or workarounds for exceptional situations
that don't have anything to do with the core logic. Too many auxiliary
conditions and code starts to get unneededly hairy.
One of the solutions is to move auxiliary logic to functions, but then it
would require the programmer to look up a function that may be used only once
in the entire program; a conditional expression makes this type of code much,
much cleaner.
Carlos Ribeiro
cribeiro at mail.inet.com.br
More information about the Python-list
mailing list