Defending the ternary operator

Paul Moore gustav at morpheus.demon.co.uk
Sun Feb 9 11:15:26 EST 2003


aahz at pythoncraft.com (Aahz) writes:

> In article <mailman.1044729201.7115.python-list at python.org>,
> Andrew Koenig  <ark at research.att.com> wrote:
>>
>>Or -- and this is what really convinces me -- I can write
>>
>>    z = x > y and x or y
>>
>>which looks really cool, and is just plain wrong, because if x is
>>0 and y is negative, the result is 0 instead of y.

Of course, the result is y, instead of 0, really. max(0, -1) should be
0, yes?

>>It's this last example that really horrifies me, because people see
>>it, think that "if a then b else c" should be written as
>>"a and b or c", and then get into trouble.

People who think like that should be corrected (or shot :-))

>>The fact that people are suggesting such circumlocutions says to me
>>that there is a use for the feature, and the fact that people are
>>suggesting incorrect circumlocutions makes my skin crawl.
>
> Then attack the problem at its roots: flame the people who suggest such
> circumlocutions instead of more Pythonic idioms.  </half-joking>

I'm not actually sure that's even half a joke. I only suggest things
like and/or or index-by-bool, to people who have already demonstrated
that they aren't willing to listen to suggestions that they look at
the problem in a wider context than just translating a single
conditional expression from the C idiom. I shouldn't give up that
easily, though - it dilutes my real point.

I haven't the patience to be a good teacher.

Paul.
-- 
This signature intentionally left blank




More information about the Python-list mailing list