[Python-Dev] re: conditional expressions
Tim Peters
tim.one@home.com
Wed, 17 Oct 2001 22:08:03 -0400
[Greg Wilson]
> I just showed the proposed conditional expression syntax
> to two colleagues, and asked them how they would write
> the "sign" function (-1 for negative, 0 for zero, +1 for
> positive) using it. Both of them wrote:
>
> s = if x < 0 then -1 elif x == 0 then 0 else 1
>
> (Well, OK, one of them tested negative, then positive, then
> returned zero as the 'else', but that's a quibble.)
Quibble? A professor in college took points off one of my programs because
I didn't test for 0 first. He explained that testing for either negative or
positive first left you vulnerable on 1's-complement machines, where
comparisons for <0 or >0 just checked the sign bit, and either form of
1's-comp 0 would pass one of those tests. I looked at him like he was
insane. He looked at me the same way. He was old enough that I suppose
he's dead now. Ha! I win <wink>.
elif-is-file-spelled-backwards-ly y'rs - tim