Defending the ternary operator

Tim Peters tim.one at comcast.net
Sat Feb 8 13:09:06 EST 2003


[Andrew Koenig]
> 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;

[David Eppstein]
> What's wrong with using s || str in this example?

C's || isn't Python's "or".  x || y in C is like Python's

    (x and 1) or (y and 1) or 0

That is, || always returns a little bool-like integer in C, regardless of
its operands' types.






More information about the Python-list mailing list