peter at engcorp.com
Tue Sep 9 17:27:46 CEST 2003
Uwe Schmitt wrote:
> Peter Hansen <peter at engcorp.com> wrote:
> > Uwe Schmitt wrote:
> >> Andrew Chalk <achalk at xxxmagnacartasoftware.com> wrote:
> >> > Is there a python equivalent of the C ternery operator?
> >> > I.e.:
> >> > fred = (x == 1) ? 12 : 15
> > I would think a *Python* equivalent to ?: should not use
> > direct equality comparison with 1. Better to drop the "==1"
> > parts in any of the above, to allow the usual Python interpretation
> > of what is True and what is False to occur.
> > ...so fred = [12, 15][not x] is sufficient.
> You made a mistake, compare :
True, sorry. I think I was thrown off by your explicit
comparison with 1 in the original, and my critical nature
homed in on that aspect, thinking you were presenting another
example of a non-Pythonic way of evaluating True/False.
I'll make this point instead: noting my mistake, and the
excited disagreement that followed in other replies (lots of
exclamation marks showing up!), this can only be seen as
yet more evidence that using any of those alternatives to
a simple if/else is rather too likely to be error prone for
anyone to prefer it.
More information about the Python-list