PEP 308 - ternary operator

Nick Trout nickNOSPAMtrout at shaw.ca
Sun Feb 16 14:49:50 EST 2003


"Erik Max Francis" <max at alcyone.com> wrote in message
news:3E4FE564.F5F1E04B at alcyone.com...
> Nick Trout wrote:
>
> > Has anyone pointed out that your can do ternary operators using:
> >
> > result = { 1: true_value, 0: false_value }[condition]
> >
> > or
> >
> > result = { 1: true_fn, 0: false_fn }[condition]()
> >
> > if they are functions.
>
> This doesn't support short-circuiting, which is the key feature of a
> true conditional operator.  The PEP itself goes into great detail about
> precisely what this feature is and why it's necessary.

I think the definition of short circuiting is a little blurred. I think it
basically means you dont want to evaluate the to values before you return
one of them, which I don't believe I'm doing in either case.

If I'd written:

  result = { 1: true_fn(), 0: false_fn() }[condition]

I wouldnt be short circuiting.

Nick









More information about the Python-list mailing list