Defending the ternary operator

Kaz Kylheku kaz at ashi.footprints.net
Mon Feb 10 15:33:11 EST 2003


David Eppstein <eppstein at ics.uci.edu> wrote in message news:<eppstein-522DB8.09320308022003 at news.service.uci.edu>...
> In article <yu99k7gaagav.fsf at europa.research.att.com>,
>  Andrew Koenig <ark at research.att.com> wrote:
> 
> > EXAMPLE 6:
> > 
> >         p = (s == NULL)? str: s;
> > 
> > This example sets a default value to substitute for s in case s is NULL.
> > The likely rewrite:
> > 
> >         if (s == NULL)
> >             p = str;
> >         else
> >             p = s;
> 
> What's wrong with using s || str in this example?

Ah, but the || operator is just another example of control-flow
embedded into an expression, and so illustrates the same need as that
which is satisfied by the ternary operator: the need to have
statement-like evaluation control within an expression.




More information about the Python-list mailing list