ternary operator

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Fri Feb 7 01:32:39 CET 2003


> From: Christopher A. Craig [mailto:list-python at ccraig.org]
> 
> Paul Foley <see at below.invalid> writes:
> 
> > On 05 Feb 2003 09:49:33 -0500, Christopher A Craig wrote:
> > 
> > > gausebec-spam at paypal.com (David Gausebeck) writes:
> > >> 2) Is there any better workaround than the ones I've 
> listed above?
> > 
> > > if a: x = b
> > > else: x = c
> > 
> > Urk.  There really ought to be a PSU death squad dedicated 
> to hunting
> > down people who write code like that!
> 
> You like (b, c)[not a] better?  I generally don't write code like

Nope. I like

    if a:
        x = b
    else:
        x = c

In fact, I have written into the Python style guide for my team explicitly to not write things like

    if a: x = b
    else: x = c

for much the same reason that I specify to not write

    if (a)
        x = b;
    else
        x = c;

or

    if (a) x = b;
    else x = c;

in C.

which is quite simply that they (a) don't convey the execution flow (indentation gives a visual representation), (b) are less extensible (they require remembering to move things around when you need more than one action) and (c) are very dangerous in the second case above.

Tim Delaney





More information about the Python-list mailing list